Skip to content

Commit d3d0deb

Browse files
Update purrr usage in tidy-data.Rmd (#1558)
* Update tidy-data.Rmd creating some reproducible example files and replacing superceded function map_dfr with list_rbind() * Tweak changes * Tweak paragraph --------- Co-authored-by: Davis Vaughan <[email protected]>
1 parent ee0c55c commit d3d0deb

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

vignettes/tidy-data.Rmd

+5-2
Original file line numberDiff line numberDiff line change
@@ -322,13 +322,16 @@ It's also common to find data values about a single type of observational unit s
322322

323323
3. Combine all tables into a single table.
324324

325-
Purrr makes this straightforward in R. The following code generates a vector of file names in a directory (`data/`) which match a regular expression (ends in `.csv`). Next we name each element of the vector with the name of the file. We do this because will preserve the names in the following step, ensuring that each row in the final data frame is labeled with its source. Finally, `map_dfr()` loops over each path, reading in the csv file and combining the results into a single data frame.
325+
Purrr makes this straightforward in R. The following theoretical code generates a vector of file names from a directory (`data/`) which match a regular expression (ends in `.csv`). Next we name each element of the vector with the name of the file. We do this because we will preserve the names in the following step, ensuring that each row in the final data frame is labeled with its source. Finally, `map()` loops over each path, reading in the csv file, and `list_rbind()` combines the results into a single data frame.
326326

327327
```{r, eval = FALSE}
328328
library(purrr)
329+
library(readr)
330+
329331
paths <- dir("data", pattern = "\\.csv$", full.names = TRUE)
330332
names(paths) <- basename(paths)
331-
map_dfr(paths, read.csv, stringsAsFactors = FALSE, .id = "filename")
333+
334+
map(paths, read_csv) %>% list_rbind(names_to = "filename")
332335
```
333336

334337
Once you have a single table, you can perform additional tidying as needed. An example of this type of cleaning can be found at <https://github.com/hadley/data-baby-names> which takes 129 yearly baby name tables provided by the US Social Security Administration and combines them into a single file.

0 commit comments

Comments
 (0)