SlideShare a Scribd company logo
1
Agenda
read data from flat or delimited files
handle column names/header
skip text/info
specify column/variable types
read specific columns/variables
•
•
•
•
•
2
Libraries
library(readr)
3
4
5
6
7
Read CSV File
read_csv('mtcars.csv')
## # A tibble: 32 x 11
## mpg cyl disp hp drat wt qsec vs am gear carb
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4
## 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4
## 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1
## 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1
## 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2
## 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1
## 7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4
## 8 24.4 4 147. 62 3.69 3.19 20 1 0 4 2
## 9 22.8 4 141. 95 3.92 3.15 22.9 1 0 4 2
## 10 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4
## # ... with 22 more rows
8
Read CSV File
read_delim('mtcars.csv', delim = ",")
## # A tibble: 32 x 11
## mpg cyl disp hp drat wt qsec vs am gear carb
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4
## 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4
## 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1
## 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1
## 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2
## 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1
## 7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4
## 8 24.4 4 147. 62 3.69 3.19 20 1 0 4 2
## 9 22.8 4 141. 95 3.92 3.15 22.9 1 0 4 2
## 10 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4
## # ... with 22 more rows
9
10
Column Names
read_csv('mtcars1.csv')
## Warning: Duplicated column names deduplicated: '4' => '4_1' [11]
## # A tibble: 31 x 11
## `21` `6` `160` `110` `3.9` `2.62` `16.46` `0` `1` `4` `4_
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <db
## 1 21 6 160 110 3.9 2.88 17.0 0 1 4
## 2 22.8 4 108 93 3.85 2.32 18.6 1 1 4
## 3 21.4 6 258 110 3.08 3.22 19.4 1 0 3
## 4 18.7 8 360 175 3.15 3.44 17.0 0 0 3
## 5 18.1 6 225 105 2.76 3.46 20.2 1 0 3
## 6 14.3 8 360 245 3.21 3.57 15.8 0 0 3
## 7 24.4 4 147. 62 3.69 3.19 20 1 0 4
## 8 22.8 4 141. 95 3.92 3.15 22.9 1 0 4
## 9 19.2 6 168. 123 3.92 3.44 18.3 1 0 4
## 10 17.8 6 168. 123 3.92 3.44 18.9 1 0 4
## # ... with 21 more rows
11
Column Names
read_csv('mtcars1.csv', col_names = FALSE)
## # A tibble: 32 x 11
## X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4
## 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4
## 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1
## 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1
## 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2
## 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1
## 7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4
## 8 24.4 4 147. 62 3.69 3.19 20 1 0 4 2
## 9 22.8 4 141. 95 3.92 3.15 22.9 1 0 4 2
## 10 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4
## # ... with 22 more rows
12
13
Skip Lines
read_csv('mtcars2.csv')
## Warning: Missing column names filled in: 'X2' [2], 'X3' [3], 'X4' [4]
## 'X5' [5], 'X6' [6], 'X7' [7], 'X8' [8], 'X9' [9], 'X10' [10], 'X11' [
## # A tibble: 51 x 11
## `The data was e~ X2 X3 X4 X5 X6 X7 X8 X9 X
## <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <
## 1 <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <
## 2 A data frame wi~ <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <
## 3 <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <
## 4 [, 1] mpg Mile~ <NA> <NA> <NA> <NA> <NA> <NA> <
## 5 [, 2] cyl Numb~ <NA> <NA> <NA> <NA> <NA> <NA> <
## 6 [, 3] disp Disp~ <NA> <NA> <NA> <NA> <NA> <NA> <
## 7 [, 4] hp Gros~ <NA> <NA> <NA> <NA> <NA> <NA> <
## 8 [, 5] drat Rear~ <NA> <NA> <NA> <NA> <NA> <NA> <
## 9 [, 6] wt Weig~ <NA> <NA> <NA> <NA> <NA> <NA> <
## 10 [, 7] qsec 1/4 ~ <NA> <NA> <NA> <NA> <NA> <NA> <
## # ... with 41 more rows, and 1 more variable: X11 <chr>
14
Skip Lines
read_csv('mtcars2.csv', skip = 19)
## # A tibble: 32 x 11
## mpg cyl disp hp drat wt qsec vs am gear carb
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4
## 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4
## 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1
## 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1
## 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2
## 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1
## 7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4
## 8 24.4 4 147. 62 3.69 3.19 20 1 0 4 2
## 9 22.8 4 141. 95 3.92 3.15 22.9 1 0 4 2
## 10 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4
## # ... with 22 more rows
15
Maximum Lines
read_csv('mtcars.csv', n_max = 20)
## # A tibble: 20 x 11
## mpg cyl disp hp drat wt qsec vs am gear carb
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4
## 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4
## 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1
## 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1
## 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2
## 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1
## 7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4
## 8 24.4 4 147. 62 3.69 3.19 20 1 0 4 2
## 9 22.8 4 141. 95 3.92 3.15 22.9 1 0 4 2
## 10 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4
## 11 17.8 6 168. 123 3.92 3.44 18.9 1 0 4 4
## 12 16.4 8 276. 180 3.07 4.07 17.4 0 0 3 3
## 13 17.3 8 276. 180 3.07 3.73 17.6 0 0 3 3
## 14 15.2 8 276. 180 3.07 3.78 18 0 0 3 3
## 15 10 4 8 472 205 2 93 5 25 18 0 0 0 3 4
16
17
Column Types
spec_csv('mtcars5.csv')
## cols(
## mpg = col_double(),
## cyl = col_double(),
## disp = col_double(),
## hp = col_double()
## )
18
19
Column Types
read_csv('mtcars5.csv',
col_types = list(col_double(), col_factor(levels = c(4, 6, 8)),
col_double(), col_integer()))
## # A tibble: 32 x 4
## mpg cyl disp hp
## <dbl> <fct> <dbl> <int>
## 1 21 6 160 110
## 2 21 6 160 110
## 3 22.8 4 108 93
## 4 21.4 6 258 110
## 5 18.7 8 360 175
## 6 18.1 6 225 105
## 7 14.3 8 360 245
## 8 24.4 4 147. 62
## 9 22.8 4 141. 95
## 10 19.2 6 168. 123
## # ... with 22 more rows
20
Skip Columns
read_csv('mtcars5.csv',
col_types = list(col_double(), col_factor(levels = c(4, 6, 8)),
col_skip(), col_integer()))
## # A tibble: 32 x 3
## mpg cyl hp
## <dbl> <fct> <int>
## 1 21 6 110
## 2 21 6 110
## 3 22.8 4 93
## 4 21.4 6 110
## 5 18.7 8 175
## 6 18.1 6 105
## 7 14.3 8 245
## 8 24.4 4 62
## 9 22.8 4 95
## 10 19.2 6 123
## # ... with 22 more rows
21
Read Specific Columns
read_csv('mtcars5.csv',
col_types = cols_only(mpg = col_double(),
cyl = col_factor(levels = c(4, 6, 8))))
## # A tibble: 32 x 2
## mpg cyl
## <dbl> <fct>
## 1 21 6
## 2 21 6
## 3 22.8 4
## 4 21.4 6
## 5 18.7 8
## 6 18.1 6
## 7 14.3 8
## 8 24.4 4
## 9 22.8 4
## 10 19.2 6
## # ... with 22 more rows
22
23
24

More Related Content

What's hot (16)

PPTX
Data manipulation and visualization in r 20190711 myanmarucsy
SmartHinJ
 
PDF
Easy HTML Tables in RStudio with Tabyl and kableExtra
Barry DeCicco
 
PDF
M12 random forest-part01
Raman Kannan
 
PDF
M11 bagging loo cv
Raman Kannan
 
PDF
M09-Cross validating-naive-bayes
Raman Kannan
 
PDF
Adventures on live partitioning
Matteo Melli
 
PPTX
Data mining in Apriori and FP-tree
Subhash Rohit
 
PDF
Computer practicals(part) Class 12
अयशकांत मिश्र
 
PDF
第3回 データフレームの基本操作 その1(解答付き)
Wataru Shito
 
PDF
第5回 様々なファイル形式の読み込みとデータの書き出し(解答付き)
Wataru Shito
 
PPTX
Python data structures
Harry Potter
 
PDF
Extending Operators in Perl with Operator::Util
Nova Patch
 
RTF
Seistech SQL code
Simon Hoyle
 
PDF
Prediction
Ban Bang
 
PPTX
Lecture5 my sql statements by okello erick
okelloerick
 
TXT
Bouncingballs sh
Ben Pope
 
Data manipulation and visualization in r 20190711 myanmarucsy
SmartHinJ
 
Easy HTML Tables in RStudio with Tabyl and kableExtra
Barry DeCicco
 
M12 random forest-part01
Raman Kannan
 
M11 bagging loo cv
Raman Kannan
 
M09-Cross validating-naive-bayes
Raman Kannan
 
Adventures on live partitioning
Matteo Melli
 
Data mining in Apriori and FP-tree
Subhash Rohit
 
Computer practicals(part) Class 12
अयशकांत मिश्र
 
第3回 データフレームの基本操作 その1(解答付き)
Wataru Shito
 
第5回 様々なファイル形式の読み込みとデータの書き出し(解答付き)
Wataru Shito
 
Python data structures
Harry Potter
 
Extending Operators in Perl with Operator::Util
Nova Patch
 
Seistech SQL code
Simon Hoyle
 
Prediction
Ban Bang
 
Lecture5 my sql statements by okello erick
okelloerick
 
Bouncingballs sh
Ben Pope
 

Similar to Read/Import data from flat/delimited files into R (20)

PPTX
Introduction to R
Stacy Irwin
 
PDF
MH prediction modeling and validation in r (1) regression 190709
Min-hyung Kim
 
PDF
Data manipulation on r
Abhik Seal
 
PDF
R Programming: Transform/Reshape Data In R
Rsquared Academy
 
PPTX
Murtaugh 2022 Appl Comp Genomics Tidyverse lecture.pptx-1.pptx
oliversen
 
PDF
Descriptive analytics in r programming language
Ashwini Mathur
 
PDF
Applied Regression Analysis using R
Tarek Dib
 
PDF
chapter3
Tarek Dib
 
PDF
第5回 様々なファイル形式の読み込みとデータの書き出し
Wataru Shito
 
PDF
Regression and Classification with R
Yanchang Zhao
 
PPTX
Cluto presentation
Roseline Antai
 
PDF
Graphics in R
Kamal Gupta Roy
 
PDF
SevillaR meetup: dplyr and magrittr
Romain Francois
 
PDF
eBPF Perf Tools 2019
Brendan Gregg
 
ODP
Beyond PHP - it's not (just) about the code
Wim Godden
 
PDF
State of GPT - Microsoft Build 2023 RD.pdf
solarobin
 
PDF
re:Invent 2019 BPF Performance Analysis at Netflix
Brendan Gregg
 
PDF
Tsukubar8
裕樹 奥田
 
PPTX
R programming language
Alberto Minetti
 
PDF
Forecasting Revenue With Stationary Time Series Models
Geoffery Mullings
 
Introduction to R
Stacy Irwin
 
MH prediction modeling and validation in r (1) regression 190709
Min-hyung Kim
 
Data manipulation on r
Abhik Seal
 
R Programming: Transform/Reshape Data In R
Rsquared Academy
 
Murtaugh 2022 Appl Comp Genomics Tidyverse lecture.pptx-1.pptx
oliversen
 
Descriptive analytics in r programming language
Ashwini Mathur
 
Applied Regression Analysis using R
Tarek Dib
 
chapter3
Tarek Dib
 
第5回 様々なファイル形式の読み込みとデータの書き出し
Wataru Shito
 
Regression and Classification with R
Yanchang Zhao
 
Cluto presentation
Roseline Antai
 
Graphics in R
Kamal Gupta Roy
 
SevillaR meetup: dplyr and magrittr
Romain Francois
 
eBPF Perf Tools 2019
Brendan Gregg
 
Beyond PHP - it's not (just) about the code
Wim Godden
 
State of GPT - Microsoft Build 2023 RD.pdf
solarobin
 
re:Invent 2019 BPF Performance Analysis at Netflix
Brendan Gregg
 
Tsukubar8
裕樹 奥田
 
R programming language
Alberto Minetti
 
Forecasting Revenue With Stationary Time Series Models
Geoffery Mullings
 
Ad

More from Rsquared Academy (20)

PDF
Handling Date & Time in R
Rsquared Academy
 
PDF
Joining Data with dplyr
Rsquared Academy
 
PDF
Variables & Data Types in R
Rsquared Academy
 
PDF
How to install & update R packages?
Rsquared Academy
 
PDF
How to get help in R?
Rsquared Academy
 
PDF
Introduction to R
Rsquared Academy
 
PDF
RMySQL Tutorial For Beginners
Rsquared Academy
 
PDF
R Markdown Tutorial For Beginners
Rsquared Academy
 
PDF
R Data Visualization Tutorial: Bar Plots
Rsquared Academy
 
PDF
R Programming: Introduction to Matrices
Rsquared Academy
 
PDF
R Programming: Introduction to Vectors
Rsquared Academy
 
PPTX
R Programming: Variables & Data Types
Rsquared Academy
 
PDF
Data Visualization With R: Learn To Combine Multiple Graphs
Rsquared Academy
 
PDF
R Data Visualization: Learn To Add Text Annotations To Plots
Rsquared Academy
 
PDF
Data Visualization With R: Learn To Modify Font Of Graphical Parameters
Rsquared Academy
 
PDF
Data Visualization With R: Learn To Modify Color Of Plots
Rsquared Academy
 
PDF
Data Visualization With R: Learn To Modify Title, Axis Labels & Range
Rsquared Academy
 
PDF
Data Visualization With R: Introduction
Rsquared Academy
 
PDF
Data Visualization With R
Rsquared Academy
 
PDF
R Programming: Mathematical Functions In R
Rsquared Academy
 
Handling Date & Time in R
Rsquared Academy
 
Joining Data with dplyr
Rsquared Academy
 
Variables & Data Types in R
Rsquared Academy
 
How to install & update R packages?
Rsquared Academy
 
How to get help in R?
Rsquared Academy
 
Introduction to R
Rsquared Academy
 
RMySQL Tutorial For Beginners
Rsquared Academy
 
R Markdown Tutorial For Beginners
Rsquared Academy
 
R Data Visualization Tutorial: Bar Plots
Rsquared Academy
 
R Programming: Introduction to Matrices
Rsquared Academy
 
R Programming: Introduction to Vectors
Rsquared Academy
 
R Programming: Variables & Data Types
Rsquared Academy
 
Data Visualization With R: Learn To Combine Multiple Graphs
Rsquared Academy
 
R Data Visualization: Learn To Add Text Annotations To Plots
Rsquared Academy
 
Data Visualization With R: Learn To Modify Font Of Graphical Parameters
Rsquared Academy
 
Data Visualization With R: Learn To Modify Color Of Plots
Rsquared Academy
 
Data Visualization With R: Learn To Modify Title, Axis Labels & Range
Rsquared Academy
 
Data Visualization With R: Introduction
Rsquared Academy
 
Data Visualization With R
Rsquared Academy
 
R Programming: Mathematical Functions In R
Rsquared Academy
 
Ad

Recently uploaded (20)

PPTX
03_Ariane BERCKMOES_Ethias.pptx_AIBarometer_release_event
FinTech Belgium
 
PPTX
big data eco system fundamentals of data science
arivukarasi
 
PPTX
MENU-DRIVEN PROGRAM ON ARUNACHAL PRADESH.pptx
manvi200807
 
PPTX
microservices-with-container-apps-dapr.pptx
vjay22
 
PPTX
How to Add Columns and Rows in an R Data Frame
subhashenia
 
PPTX
Generative AI Boost Data Governance and Quality- Tejasvi Addagada
Tejasvi Addagada
 
PPTX
thid ppt defines the ich guridlens and gives the information about the ICH gu...
shaistabegum14
 
PPTX
美国史蒂文斯理工学院毕业证书{SIT学费发票SIT录取通知书}哪里购买
Taqyea
 
PPTX
BinarySearchTree in datastructures in detail
kichokuttu
 
PPTX
Presentation.pptx hhgihyugyygyijguuffddfffffff
abhiruppal2007
 
PDF
A GraphRAG approach for Energy Efficiency Q&A
Marco Brambilla
 
PDF
Group 5_RMB Final Project on circular economy
pgban24anmola
 
PDF
2025 Global Data Summit - FOM with AI.pdf
Marco Wobben
 
PPTX
Artificial intelligence Presentation1.pptx
SaritaMahajan5
 
PPTX
SHREYAS25 INTERN-I,II,III PPT (1).pptx pre
swapnilherage
 
PPTX
办理学历认证InformaticsLetter新加坡英华美学院毕业证书,Informatics成绩单
Taqyea
 
PDF
TESDA License NC II PC Operations TESDA, Office Productivity
MELJUN CORTES
 
DOCX
🧩 1. Solvent R-WPS Office work scientific
NohaSalah45
 
PDF
Unlocking Insights: Introducing i-Metrics Asia-Pacific Corporation and Strate...
Janette Toral
 
PDF
Orchestrating Data Workloads With Airflow.pdf
ssuserae5511
 
03_Ariane BERCKMOES_Ethias.pptx_AIBarometer_release_event
FinTech Belgium
 
big data eco system fundamentals of data science
arivukarasi
 
MENU-DRIVEN PROGRAM ON ARUNACHAL PRADESH.pptx
manvi200807
 
microservices-with-container-apps-dapr.pptx
vjay22
 
How to Add Columns and Rows in an R Data Frame
subhashenia
 
Generative AI Boost Data Governance and Quality- Tejasvi Addagada
Tejasvi Addagada
 
thid ppt defines the ich guridlens and gives the information about the ICH gu...
shaistabegum14
 
美国史蒂文斯理工学院毕业证书{SIT学费发票SIT录取通知书}哪里购买
Taqyea
 
BinarySearchTree in datastructures in detail
kichokuttu
 
Presentation.pptx hhgihyugyygyijguuffddfffffff
abhiruppal2007
 
A GraphRAG approach for Energy Efficiency Q&A
Marco Brambilla
 
Group 5_RMB Final Project on circular economy
pgban24anmola
 
2025 Global Data Summit - FOM with AI.pdf
Marco Wobben
 
Artificial intelligence Presentation1.pptx
SaritaMahajan5
 
SHREYAS25 INTERN-I,II,III PPT (1).pptx pre
swapnilherage
 
办理学历认证InformaticsLetter新加坡英华美学院毕业证书,Informatics成绩单
Taqyea
 
TESDA License NC II PC Operations TESDA, Office Productivity
MELJUN CORTES
 
🧩 1. Solvent R-WPS Office work scientific
NohaSalah45
 
Unlocking Insights: Introducing i-Metrics Asia-Pacific Corporation and Strate...
Janette Toral
 
Orchestrating Data Workloads With Airflow.pdf
ssuserae5511
 

Read/Import data from flat/delimited files into R

  • 1. 1
  • 2. Agenda read data from flat or delimited files handle column names/header skip text/info specify column/variable types read specific columns/variables • • • • • 2
  • 4. 4
  • 5. 5
  • 6. 6
  • 7. 7
  • 8. Read CSV File read_csv('mtcars.csv') ## # A tibble: 32 x 11 ## mpg cyl disp hp drat wt qsec vs am gear carb ## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> ## 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4 ## 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4 ## 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1 ## 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1 ## 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2 ## 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1 ## 7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4 ## 8 24.4 4 147. 62 3.69 3.19 20 1 0 4 2 ## 9 22.8 4 141. 95 3.92 3.15 22.9 1 0 4 2 ## 10 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4 ## # ... with 22 more rows 8
  • 9. Read CSV File read_delim('mtcars.csv', delim = ",") ## # A tibble: 32 x 11 ## mpg cyl disp hp drat wt qsec vs am gear carb ## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> ## 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4 ## 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4 ## 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1 ## 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1 ## 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2 ## 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1 ## 7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4 ## 8 24.4 4 147. 62 3.69 3.19 20 1 0 4 2 ## 9 22.8 4 141. 95 3.92 3.15 22.9 1 0 4 2 ## 10 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4 ## # ... with 22 more rows 9
  • 10. 10
  • 11. Column Names read_csv('mtcars1.csv') ## Warning: Duplicated column names deduplicated: '4' => '4_1' [11] ## # A tibble: 31 x 11 ## `21` `6` `160` `110` `3.9` `2.62` `16.46` `0` `1` `4` `4_ ## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <db ## 1 21 6 160 110 3.9 2.88 17.0 0 1 4 ## 2 22.8 4 108 93 3.85 2.32 18.6 1 1 4 ## 3 21.4 6 258 110 3.08 3.22 19.4 1 0 3 ## 4 18.7 8 360 175 3.15 3.44 17.0 0 0 3 ## 5 18.1 6 225 105 2.76 3.46 20.2 1 0 3 ## 6 14.3 8 360 245 3.21 3.57 15.8 0 0 3 ## 7 24.4 4 147. 62 3.69 3.19 20 1 0 4 ## 8 22.8 4 141. 95 3.92 3.15 22.9 1 0 4 ## 9 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 ## 10 17.8 6 168. 123 3.92 3.44 18.9 1 0 4 ## # ... with 21 more rows 11
  • 12. Column Names read_csv('mtcars1.csv', col_names = FALSE) ## # A tibble: 32 x 11 ## X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 ## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> ## 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4 ## 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4 ## 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1 ## 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1 ## 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2 ## 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1 ## 7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4 ## 8 24.4 4 147. 62 3.69 3.19 20 1 0 4 2 ## 9 22.8 4 141. 95 3.92 3.15 22.9 1 0 4 2 ## 10 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4 ## # ... with 22 more rows 12
  • 13. 13
  • 14. Skip Lines read_csv('mtcars2.csv') ## Warning: Missing column names filled in: 'X2' [2], 'X3' [3], 'X4' [4] ## 'X5' [5], 'X6' [6], 'X7' [7], 'X8' [8], 'X9' [9], 'X10' [10], 'X11' [ ## # A tibble: 51 x 11 ## `The data was e~ X2 X3 X4 X5 X6 X7 X8 X9 X ## <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> < ## 1 <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> < ## 2 A data frame wi~ <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> < ## 3 <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> < ## 4 [, 1] mpg Mile~ <NA> <NA> <NA> <NA> <NA> <NA> < ## 5 [, 2] cyl Numb~ <NA> <NA> <NA> <NA> <NA> <NA> < ## 6 [, 3] disp Disp~ <NA> <NA> <NA> <NA> <NA> <NA> < ## 7 [, 4] hp Gros~ <NA> <NA> <NA> <NA> <NA> <NA> < ## 8 [, 5] drat Rear~ <NA> <NA> <NA> <NA> <NA> <NA> < ## 9 [, 6] wt Weig~ <NA> <NA> <NA> <NA> <NA> <NA> < ## 10 [, 7] qsec 1/4 ~ <NA> <NA> <NA> <NA> <NA> <NA> < ## # ... with 41 more rows, and 1 more variable: X11 <chr> 14
  • 15. Skip Lines read_csv('mtcars2.csv', skip = 19) ## # A tibble: 32 x 11 ## mpg cyl disp hp drat wt qsec vs am gear carb ## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> ## 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4 ## 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4 ## 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1 ## 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1 ## 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2 ## 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1 ## 7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4 ## 8 24.4 4 147. 62 3.69 3.19 20 1 0 4 2 ## 9 22.8 4 141. 95 3.92 3.15 22.9 1 0 4 2 ## 10 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4 ## # ... with 22 more rows 15
  • 16. Maximum Lines read_csv('mtcars.csv', n_max = 20) ## # A tibble: 20 x 11 ## mpg cyl disp hp drat wt qsec vs am gear carb ## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> ## 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4 ## 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4 ## 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1 ## 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1 ## 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2 ## 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1 ## 7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4 ## 8 24.4 4 147. 62 3.69 3.19 20 1 0 4 2 ## 9 22.8 4 141. 95 3.92 3.15 22.9 1 0 4 2 ## 10 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4 ## 11 17.8 6 168. 123 3.92 3.44 18.9 1 0 4 4 ## 12 16.4 8 276. 180 3.07 4.07 17.4 0 0 3 3 ## 13 17.3 8 276. 180 3.07 3.73 17.6 0 0 3 3 ## 14 15.2 8 276. 180 3.07 3.78 18 0 0 3 3 ## 15 10 4 8 472 205 2 93 5 25 18 0 0 0 3 4 16
  • 17. 17
  • 18. Column Types spec_csv('mtcars5.csv') ## cols( ## mpg = col_double(), ## cyl = col_double(), ## disp = col_double(), ## hp = col_double() ## ) 18
  • 19. 19
  • 20. Column Types read_csv('mtcars5.csv', col_types = list(col_double(), col_factor(levels = c(4, 6, 8)), col_double(), col_integer())) ## # A tibble: 32 x 4 ## mpg cyl disp hp ## <dbl> <fct> <dbl> <int> ## 1 21 6 160 110 ## 2 21 6 160 110 ## 3 22.8 4 108 93 ## 4 21.4 6 258 110 ## 5 18.7 8 360 175 ## 6 18.1 6 225 105 ## 7 14.3 8 360 245 ## 8 24.4 4 147. 62 ## 9 22.8 4 141. 95 ## 10 19.2 6 168. 123 ## # ... with 22 more rows 20
  • 21. Skip Columns read_csv('mtcars5.csv', col_types = list(col_double(), col_factor(levels = c(4, 6, 8)), col_skip(), col_integer())) ## # A tibble: 32 x 3 ## mpg cyl hp ## <dbl> <fct> <int> ## 1 21 6 110 ## 2 21 6 110 ## 3 22.8 4 93 ## 4 21.4 6 110 ## 5 18.7 8 175 ## 6 18.1 6 105 ## 7 14.3 8 245 ## 8 24.4 4 62 ## 9 22.8 4 95 ## 10 19.2 6 123 ## # ... with 22 more rows 21
  • 22. Read Specific Columns read_csv('mtcars5.csv', col_types = cols_only(mpg = col_double(), cyl = col_factor(levels = c(4, 6, 8)))) ## # A tibble: 32 x 2 ## mpg cyl ## <dbl> <fct> ## 1 21 6 ## 2 21 6 ## 3 22.8 4 ## 4 21.4 6 ## 5 18.7 8 ## 6 18.1 6 ## 7 14.3 8 ## 8 24.4 4 ## 9 22.8 4 ## 10 19.2 6 ## # ... with 22 more rows 22
  • 23. 23
  • 24. 24