39

I have a list of data frames and have given each element in the list (e.g. each data frame) a name:

e.g.

df1 <- data.frame(x = c(1:5), y = c(11:15))  
df2 <- data.frame(x = c(1:5), y = c(11:15))  
mylist <- list(A = df1, B = df2)  

I have a function that I want to apply to each data frame; In this function, I want to include a line to write the results to file (eventually I want to do more complicated things like save plots of the correlation between two variables for each data frame but thought I'd start simple)

e.g.

NewVar <- function(mydata, whichVar, i) {  
mydata$newVar <- mydata[, whichVar] + 1  
write.csv(mydata, file = i)  
}

I want to use lapply() to apply this function to each data frame in my list

something like:

hh<-lapply(mylist, NewVar, whichVar = "y")

I can't figure out how to assign the "i" within the context of lapply so that i iterates over the names in the list of data frames, saving multiple files with different names (in this case, two files named A and B) that correspond with the modified data frames.

MERose
  • 4,048
  • 7
  • 53
  • 79
user2414840
  • 721
  • 1
  • 7
  • 15
  • 2
    I know you said you're trying to avoid using a for loop but I think it's right to use a for loop in this case. I don't like using `lapply()` if nothing is being returned. Something like `l_ply()` from the `plyr` package is probably more appropriate. Or just a for loop. – Ciarán Tobin Jun 10 '13 at 10:33

3 Answers3

14

There are many options. For example:

  lapply(names(mylist),
         function(x)write.csv(mylist[x],
                              file =paste0(x,'.csv')))

or using indexes :

 lapply(seq_along(mylist),
     function(i)write.csv(mylist[i],
                          file =paste0(names(mylist)[i],'.csv')))
agstudy
  • 119,832
  • 17
  • 199
  • 261
  • 1
    Thanks for the reply...However, I tried both of these options and they don't seem to work for a function that is defined outside of the lapply command. My real function needs to do a bunch of analyses and then save all the results so no option for defining it in one line. – user2414840 Jun 10 '13 at 16:16
14

It will work with the following lapply call:

lapply(names(mylist), function(x) NewVar(mylist[[x]], "y", x))
Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
0

I had the same problem and I solved it using l_ply() function from plyr() package.

my_list <- list(df1,df2) my_function <- function(i) {...} library(plyr) l_ply(my_list, my_function)

Ang
  • 1