Reading data from multiple files

  • read_csv() can read data stored in the same directory split across multiple files instead of being contained in a single file -> stack them on top of each other in a single data frame.

  • id argument adds a new column called file -> identifies the file the data come from:

sales_files <- c("data/01-sales.csv", "data/02-sales.csv", "data/03-sales.csv")
head(stacked_csv <- read_csv(sales_files, id = "file"), 5)
## Rows: 19 Columns: 6
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): month
## dbl (4): year, brand, item, n
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## # A tibble: 5 × 6
##   file              month    year brand  item     n
##   <chr>             <chr>   <dbl> <dbl> <dbl> <dbl>
## 1 data/01-sales.csv January  2019     1  1234     3
## 2 data/01-sales.csv January  2019     1  8721     9
## 3 data/01-sales.csv January  2019     1  1822     2
## 4 data/01-sales.csv January  2019     2  3333     1
## 5 data/01-sales.csv January  2019     2  2156     9
class(stacked_csv)
## [1] "spec_tbl_df" "tbl_df"      "tbl"         "data.frame"