Iterating on iteration
This commit is contained in:
@@ -233,7 +233,7 @@ There are a few good reasons to favor readr functions over the base equivalents:
|
||||
read_csv("a;b\n1;3")
|
||||
```
|
||||
|
||||
## Reading data from multiple files
|
||||
## Reading data from multiple files {#sec-readr-directory}
|
||||
|
||||
Sometimes your data is split across multiple files instead of being contained in a single file.
|
||||
For example, you might have sales data for multiple months, with each month's data in a separate file: `01-sales.csv` for January, `02-sales.csv` for February, and `03-sales.csv` for March.
|
||||
@@ -248,11 +248,11 @@ With the additional `id` parameter we have added a new column called `file` to t
|
||||
This is especially helpful in circumstances where the files you're reading in do not have an identifying column that can help you trace the observations back to their original sources.
|
||||
|
||||
If you have many files you want to read in, it can get cumbersome to write out their names as a list.
|
||||
Instead, you can use the `dir_ls()` function from the [fs](https://fs.r-lib.org/) package to find the files for you by matching a pattern in the file names.
|
||||
Instead, you can use the base `dir()` function to find the files for you by matching a pattern in the file names.
|
||||
You'll learn more about these patterns in @sec-strings.
|
||||
|
||||
```{r}
|
||||
library(fs)
|
||||
sales_files <- dir_ls("data", glob = "*sales.csv")
|
||||
sales_files <- dir("data", pattern = "sales\\.csv$", full.names = TRUE)
|
||||
sales_files
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user