Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using lapply to apply a function over list of data frames and saving output to files with different names

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.

like image 270
user2414840 Avatar asked Jun 10 '13 06:06

user2414840


People also ask

What does the Lapply function do?

The lapply() function helps us in applying functions on list objects and returns a list object of the same length. The lapply() function in the R Language takes a list, vector, or data frame as input and gives output in the form of a list object.

Can you use Lapply on a Dataframe?

lapply() on a data frame. If, instead of a list, you had a data frame of stock returns, could you still use lapply() ? Yes! Perhaps surprisingly, data frames are actually lists under the hood, and an lapply() call would apply the function to each column of the data frame.

Which command applies the function to each element in the given vector and returns a list that contains all the results?

Description. lapply returns a list of the same length as X , each element of which is the result of applying FUN to the corresponding element of X .

How do I apply a function to a list in R?

lapply() function in R Programming Language is used to apply a function over a list of elements. lapply() function is used with a list and performs the following operations: lapply(List, length): Returns the length of objects present in the list, List.


Video Answer


2 Answers

It will work with the following lapply call:

lapply(names(mylist), function(x) NewVar(mylist[[x]], "y", x)) 
like image 86
Sven Hohenstein Avatar answered Oct 13 '22 19:10

Sven Hohenstein


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'))) 
like image 24
agstudy Avatar answered Oct 13 '22 20:10

agstudy