readr: read_delim

Introduction to Importing Data in R

Filip Schouwenaars

Instructor, DataCamp

states2.txt

states2.txt

state/capital/pop_mill/area_sqm
South Dakota/Pierre/0.853/77116
New York/Albany/19.746/54555
Oregon/Salem/3.970/98381
Vermont/Montpelier/0.627/9616
Hawaii/Honolulu/1.420/10931
Introduction to Importing Data in R

states2.txt

read.table("states2.txt", header = TRUE, sep = "/")

         state    capital pop_mill area_sqm
1 South Dakota     Pierre    0.853    77116
2     New York     Albany   19.746    54555
3       Oregon      Salem    3.970    98381
4      Vermont Montpelier    0.627     9616
5       Hawaii   Honolulu    1.420    10931
read_delim("states2.txt", delim = "/")
# A tibble: 5 x 4
         state    capital pop_mill area_sqm
         <chr>      <chr>    <dbl>    <int>
1 South Dakota     Pierre    0.853    77116
2     New York     Albany   19.746    54555
3       Oregon      Salem    3.970    98381
4      Vermont Montpelier    0.627     9616
5       Hawaii   Honolulu    1.420    10931
Introduction to Importing Data in R

col_names

states3.txt

South Dakota/Pierre/0.853/77116
New York/Albany/19.746/54555
Oregon/Salem/3.970/98381
Vermont/Montpelier/0.627/9616
Hawaii/Honolulu/1.420/10931
Introduction to Importing Data in R

col_names

read_delim("states3.txt", delim = "/", col_names = FALSE)

           X1         X2     X3    X4
         <chr>      <chr>  <dbl> <int>
1 South Dakota     Pierre  0.853 77116
2     New York     Albany 19.746 54555
3       Oregon      Salem  3.970 98381
4      Vermont Montpelier  0.627  9616
5       Hawaii   Honolulu  1.420 10931
read_delim("states3.txt", delim = "/",              
           col_names = c("state", "city", "pop", "area"))
        state       city    pop  area
         <chr>      <chr>  <dbl> <int>
1 South Dakota     Pierre  0.853 77116
2     New York     Albany 19.746 54555
3       Oregon      Salem  3.970 98381
4      Vermont Montpelier  0.627  9616
5       Hawaii   Honolulu  1.420 10931
Introduction to Importing Data in R

col_types

read_delim("states2.txt", delim = "/")

         state    capital pop_mill area_sqm
         <chr>      <chr>    <dbl>    <int>
1 South Dakota     Pierre    0.853    77116
2     New York     Albany   19.746    54555
3       Oregon      Salem    3.970    98381
4      Vermont Montpelier    0.627     9616
5       Hawaii   Honolulu    1.420    10931
read_delim("states2.txt", delim = "/", col_types = "ccdd")
         state    capital pop_mill area_sqm
         <chr>      <chr>    <dbl>    <dbl>
1 South Dakota     Pierre    0.853    77116
2     New York     Albany   19.746    54555
3       Oregon      Salem    3.970    98381
4      Vermont Montpelier    0.627     9616
5       Hawaii   Honolulu    1.420    10931
Introduction to Importing Data in R

skip and n_max

read_delim("states2.txt", delim = "/",
             skip = 2, n_max = 3)
# A tibble: 3 x 4
  New York     Albany 19.746 54555
     <chr>      <chr>  <dbl> <int>
1   Oregon      Salem  3.970 98381
2  Vermont Montpelier  0.627  9616
3   Hawaii   Honolulu  1.420 10931
read_delim("states2.txt", delim = "/",
             col_names = c("state", "city", "pop", "area"),
             skip = 2, n_max = 3)
# A tibble: 3 x 4
     state       city    pop  area
     <chr>      <chr>  <dbl> <int>
1 New York     Albany 19.746 54555
2   Oregon      Salem  3.970 98381
3  Vermont Montpelier  0.627  9616
Introduction to Importing Data in R

Let's practice!

Introduction to Importing Data in R

Preparing Video For Download...