I have a list of 6 dataframes, and I would like to create a list of 6 lists of 24 dataframes, the 24 dataframes being subsets of the original 6 dataframes.
Here is a shorter example of what I'm trying to do:
months <- c(0:35)
product<- c(112:147)
index <- rnorm(36)
df1 <- data.frame(months, product, index)
product2<- c(212:247)
index2 <- rnorm(36)
df2 <- data.frame(months, product2, index2)
product3<- c(312:347)
index3 <- rnorm(36)
df3 <- data.frame(months, product3, index3)
dflist <- list(df1, df2, df3)
#Creating the subset function
subfun<-function(x,y,z) { subset(x,y>=z & y<=z+12) }
#Creating a list of 24 new dataframes from each existing dataframe
regdflist <- lapply(dflist, function(df) { lapply(1:24, function(x) subfun (df, df[["months"]], x-1)) df })
When I run this, it creates a new list regdflist, which is identical to dflist, and doesn't create the new subset dataframes.
I am trying to use an lapply function within an lapply function, to subset the data, and create a new list within the original list. Is that possible?
Is anyone able to advise where I've gone wrong here?
This is my earlier question, where I applied the subfun function to only one dataframe. I'm now trying to adapt this for multiple dataframes.
Making a list of dataframes which are a subset of one dataframe using R
I have also seen this answer: R create new list of dataframes of a list of dataframes But I think what he's doing is similar to what I've tried to do, and I can't make it work.
Thanks very much for your help
subsetsays: "Warning This is a convenience function intended for use interactively. For programming it is better to use the standard subsetting functions like [, and in particular the non-standard evaluation of argument subset can have unanticipated consequences."