0

I have data frames in a list a and I want to use a loop to save these as both rda and write as dta. I don't get why I get the error message that object data frame cannot be found:

for (f in a) {
  for (name in 1:length(filenames)) {
    save(as.data.frame(f),file = paste("~/Dropbox/Data_Insert/Panels/",name,end_rda,sep=""))
    write.dta(as.data.frame(f),file = paste("~/Dropbox/Data_Insert/Panels/",name,end_dta,sep=""))
         }
}

Error in save(as.data.frame(f), file = paste("~/Dropbox/Data_Insert/Panels/",  : 
  object ‘as.data.frame(f)’ not found

So by f, this would be indexing the data frame in the list? I did as.data.frame(f) because when I only used f, I got the message:

The object "dataframe" must have class data.frame

I changed the code to for f in a, but it still returns an error saying that as.data.frame(f) not found.

halo09876
  • 2,725
  • 12
  • 51
  • 71
  • 1
    In `for (f in 1:length(a))` the loop variable `f` becomes 1, 2, etc. Maybe you want `for (f in a)` – Rui Barradas Nov 19 '18 at 17:56
  • @RuiBarradas I still get the message: object ‘as.data.frame(f)’ not found – halo09876 Nov 19 '18 at 17:58
  • Yes, you have two choices with a for loop in R. (a) you can loop over the index and use indexing, `for (f in 1:length(a)) {save(as.data.frame(a[[f]]))...` or (b) loop over the objects themselves `for (f in a) {save(as.data.frame(f))...`. Both have pros and cons, but you must pick one. Your code above tries to do both. – Gregor Thomas Nov 19 '18 at 17:59
  • Also, why a nested loop here? It seems you are saving copies of the same `f` data frame under every `name` in `filenames`? – Gregor Thomas Nov 19 '18 at 18:01
  • @Gregor Yes I have filenames that match each data frames in the list – halo09876 Nov 19 '18 at 18:02
  • @Gregor I tried for f in a, but the code still returns object ‘as.data.frame(f)’ not found – halo09876 Nov 19 '18 at 18:02
  • And lastly, `save` uses non-standard evaluation so you can't coerce in the `save` call. I would suggest `for(...) {df = as.data.frame(f); save(df) ...}`. – Gregor Thomas Nov 19 '18 at 18:02
  • You have filenames that match each data frame, but by nesting the loops you are doing *every combination* not just matched combinations. If you have data frames `a, b, c` and file names `a.rda, b.rda, c.rda` your code is set up to save `a` as `a.rda`, then save `a` again as `b.rda`, then save `a` a third time as `c.rda`, then save `b` as `a.rda`.... – Gregor Thomas Nov 19 '18 at 18:04

1 Answers1

0

I think that this is what you are trying to do. I assume that a is a list of data frames and filenames is a character vector of the same length.

for (i in 1:length(a)) {
  to_save = as.data.frame(a[[i]])
  save(to_save, file = paste0("~/Dropbox/Data_Insert/Panels/", filenames[i], end_rda))
  write.dta(to_save, file = paste0("~/Dropbox/Data_Insert/Panels/", filenames[i], end_dta))
}

Note that save preserves the name of the R object, so when you load any of these files it will be loaded into the workspace with the name to_save. This seems bad. For individual R objects I would strongly encourage you to use saveRDS and create .RDS files instead of save. See, e.g., Ricardo's answer to this question for an example of Rda vs RDS.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294