0

Happy Holidays!! I am a relatively new R user, and I have not yet used lists. I am using R to pull information from 159 excel files (one per county) with up to 27 separate sheets per excel file. I would like to save these to 159 tables using a loop. I have searched through StackOverflow, and I see many examples, but I am so new that I don't understand much of the code.

#vector of the county names(shortened for this example).
county <- c("Appling", "Atkinson", "Bacon", "Baker")

for (i in 1:4) {
#lots of pulling from Excel into temp data frames
#now need to have a unique name for each county table, preferably just
#the name of the county (e.g. Appling)
unique_name <- rbind(temp1, temp2, temp3)}

Ideally, I would like to end up with the following data frames: Appling Atkinson Bacon Baker

Thanks so much for any help! Jennifer

  • After the last step `write.csv(unique_name, file = paste0(county[i], '.csv'), row.names = FALSE, quote = fALSE)` – akrun Dec 25 '18 at 01:29
  • 1
    Look for examples where you are reading a collection of files into a LIST of data frames, not a collection of individual data frames. Saving files to separately named objects is quite possible one of the most common beginner mistakes in R. – joran Dec 25 '18 at 01:43
  • 1
    This is the canonical example: https://stackoverflow.com/q/11433432/324364 – joran Dec 25 '18 at 01:46

1 Answers1

0

Maybe this is want you want:

county <- c("Appling", "Atkinson", "Bacon", "Baker")
for (i in 1:4) {
  # ...
  assign(county[i], rbind(temp1, temp2, temp3))
}

====

Well, as the comments, the better solution is this:

county.list <- list
county <- c("Appling", "Atkinson", "Bacon", "Baker")
for (i in 1:4) {
  # ...
  county.list[[county[i]]] <- rbind(temp1, temp2, temp3)
}
Navy Cheng
  • 573
  • 4
  • 14
  • I don't understand why your answer was downvoted. It was exactly the answer I was asking for. @Joran, thank you for your comments, also. As an R beginner, it would be helpful to hear about "why" saving files to separately named objects is a mistake. – JenniferAnn Dec 25 '18 at 03:32
  • 2
    Please see @Joran's comment above. Why flood your global environment with 159 (next week it might by 500+) similarly structured, separate data frames (quite a bit of maintenance) when you could use **one** named (i.e., indexed) list of many elements? You lose no functionality of data frame if it is saved in a list: `mylist$Appling` vs `Appling`. Plus you can run operations on all items of list much easier than separate objects such as *apply* family. Using `assign` runs counter to the functional programming of R. – Parfait Dec 25 '18 at 04:14
  • If your data frames have a *county* column, use `split` or `by` for a list of data frames: `large_df <- rbind(temp1, temp2, temp3); mylist <- split(large_df, large_df$county)` – Parfait Dec 25 '18 at 04:16
  • This answer is what @JenniferAnn want, and Parfait's suggest is better. – Navy Cheng Dec 25 '18 at 13:08
  • This makes perfect sense now. Thank you @Parfait! I am so happy to learn. I will now go learn more about putting data frames into lists. And thank you @Navy Cheng for giving the "better solution". Best regards! – JenniferAnn Dec 26 '18 at 20:26