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

PDF
Introduction to tibbles
PDF
Data Wrangling with dplyr
PDF
Explore Data using dplyr
PDF
Writing Readable Code with Pipes
PDF
Practical Introduction to Web scraping using R
PDF
Read data from Excel spreadsheets into R
PDF
Market Basket Analysis in R
PDF
Chapter 2: R tutorial Handbook for Data Science and Machine Learning Practiti...
Introduction to tibbles
Data Wrangling with dplyr
Explore Data using dplyr
Writing Readable Code with Pipes
Practical Introduction to Web scraping using R
Read data from Excel spreadsheets into R
Market Basket Analysis in R
Chapter 2: R tutorial Handbook for Data Science and Machine Learning Practiti...

What's hot (16)

PPTX
Data manipulation and visualization in r 20190711 myanmarucsy
PDF
Easy HTML Tables in RStudio with Tabyl and kableExtra
PDF
M12 random forest-part01
PDF
M11 bagging loo cv
PDF
M09-Cross validating-naive-bayes
PDF
Adventures on live partitioning
PPTX
Data mining in Apriori and FP-tree
PDF
Computer practicals(part) Class 12
PDF
第3回 データフレームの基本操作 その1(解答付き)
PDF
第5回 様々なファイル形式の読み込みとデータの書き出し(解答付き)
PPTX
Python data structures
PDF
Extending Operators in Perl with Operator::Util
RTF
Seistech SQL code
PDF
Prediction
PPTX
Lecture5 my sql statements by okello erick
TXT
Bouncingballs sh
Data manipulation and visualization in r 20190711 myanmarucsy
Easy HTML Tables in RStudio with Tabyl and kableExtra
M12 random forest-part01
M11 bagging loo cv
M09-Cross validating-naive-bayes
Adventures on live partitioning
Data mining in Apriori and FP-tree
Computer practicals(part) Class 12
第3回 データフレームの基本操作 その1(解答付き)
第5回 様々なファイル形式の読み込みとデータの書き出し(解答付き)
Python data structures
Extending Operators in Perl with Operator::Util
Seistech SQL code
Prediction
Lecture5 my sql statements by okello erick
Bouncingballs sh
Ad

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

PPTX
Introduction to R
PDF
Data import-cheatsheet
PPTX
fINAL Lesson_5_Data_Manipulation_using_R_v1.pptx
PDF
R for you
PDF
MH prediction modeling and validation in r (1) regression 190709
PDF
Practical Data Science : Data Cleaning and Summarising
PDF
tidyr.pdf
PPTX
Murtaugh 2022 Appl Comp Genomics Tidyverse lecture.pptx-1.pptx
PPTX
Unit I - introduction to r language 2.pptx
PDF
9. R data-import data-export
PDF
R gráfico
PDF
Dplyr v2 . Exploratory data analysis.pdf
PDF
Dplyr v2 . Exploratory data analysis.pdf
PDF
Stata cheatsheet transformation
PDF
Data transformation-cheatsheet
PPTX
Data Exploration in R.pptx
PDF
7. Data Import – Data Export
 
PDF
Stata Cheat Sheets (all)
PPTX
Lab 2 - Managing Data in R Basic Conecpt.pptx
PDF
Data manipulation on r
Introduction to R
Data import-cheatsheet
fINAL Lesson_5_Data_Manipulation_using_R_v1.pptx
R for you
MH prediction modeling and validation in r (1) regression 190709
Practical Data Science : Data Cleaning and Summarising
tidyr.pdf
Murtaugh 2022 Appl Comp Genomics Tidyverse lecture.pptx-1.pptx
Unit I - introduction to r language 2.pptx
9. R data-import data-export
R gráfico
Dplyr v2 . Exploratory data analysis.pdf
Dplyr v2 . Exploratory data analysis.pdf
Stata cheatsheet transformation
Data transformation-cheatsheet
Data Exploration in R.pptx
7. Data Import – Data Export
 
Stata Cheat Sheets (all)
Lab 2 - Managing Data in R Basic Conecpt.pptx
Data manipulation on r
Ad

More from Rsquared Academy (20)

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

Recently uploaded (20)

PDF
Lecture1 pattern recognition............
PPTX
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
PPTX
Microsoft-Fabric-Unifying-Analytics-for-the-Modern-Enterprise Solution.pptx
PDF
annual-report-2024-2025 original latest.
PDF
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.pdf
PPTX
IB Computer Science - Internal Assessment.pptx
PDF
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
PDF
Foundation of Data Science unit number two notes
PPTX
Supervised vs unsupervised machine learning algorithms
PPTX
1_Introduction to advance data techniques.pptx
PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
PPTX
01_intro xxxxxxxxxxfffffffffffaaaaaaaaaaafg
PPTX
Business Ppt On Nestle.pptx huunnnhhgfvu
PDF
Mega Projects Data Mega Projects Data
PPTX
Database Infoormation System (DBIS).pptx
PPTX
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
PPTX
ALIMENTARY AND BILIARY CONDITIONS 3-1.pptx
PDF
Business Analytics and business intelligence.pdf
PPTX
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
PPTX
IBA_Chapter_11_Slides_Final_Accessible.pptx
Lecture1 pattern recognition............
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
Microsoft-Fabric-Unifying-Analytics-for-the-Modern-Enterprise Solution.pptx
annual-report-2024-2025 original latest.
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.pdf
IB Computer Science - Internal Assessment.pptx
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
Foundation of Data Science unit number two notes
Supervised vs unsupervised machine learning algorithms
1_Introduction to advance data techniques.pptx
Acceptance and paychological effects of mandatory extra coach I classes.pptx
01_intro xxxxxxxxxxfffffffffffaaaaaaaaaaafg
Business Ppt On Nestle.pptx huunnnhhgfvu
Mega Projects Data Mega Projects Data
Database Infoormation System (DBIS).pptx
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
ALIMENTARY AND BILIARY CONDITIONS 3-1.pptx
Business Analytics and business intelligence.pdf
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
IBA_Chapter_11_Slides_Final_Accessible.pptx

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