Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional material on storing all data files to looping over files #945

Open
ethanwhite opened this issue May 21, 2022 · 0 comments
Open

Comments

@ethanwhite
Copy link
Member

From my comment on YouTube:

  1. make data a list and store each data frame loaded by the loop as one position in that list, so the first time through the loop the data frame gets stored in the first position in the list, the second time through the loop it gets stored in the second position, and so on. You can see some of the basic ideas in our Looping By Index video: https://www.youtube.com/watch?v=vWj5rypEZ4U&t=9s The modified version of the code would look like:
data <- vector(mode = "list", length = length(data_files))
for (i in 1:length(data_files)){
  df <- read.csv(data_files[i])
  data[[i]] <- df
}
  1. Load all of the data into a single data frame. Technically you can do this using a loop by each time through the loop loading the new data and then appending it (using rbind) to the bottom of the data frame, but it's now easier to do it with a single command from the purrr package:
data <- map_dfr(data_files, read.csv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant