Create a list of a list of dataframes, by subsetting a list of dataframes in R

半城伤御伤魂 提交于 2019-12-12 04:22:27

问题


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


回答1:


subfun <- function(x,y,z) {  
    x[x[[y]] >= z & x[[y]] <= z+12,]
}

a_df_list <- lapply(dflist, function(i){
    lapply(1:24, function(j){
        subfun(x = i, y = 1, z = j - 1)
    })
})

mapply(length, a_df_list)

[1] 24 24 24

str(a_df_list[[1]])

List of 24
 $ :'data.frame':   13 obs. of  3 variables:
  ..$ months : int [1:13] 0 1 2 3 4 5 6 7 8 9 ...
  ..$ product: int [1:13] 112 113 114 115 116 117 118 119 120 121 ...
  ..$ index  : num [1:13] -0.00928 -0.18127 -0.67568 0.31983 0.12634 ...
 $ :'data.frame':   13 obs. of  3 variables:
  ..$ months : int [1:13] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ product: int [1:13] 113 114 115 116 117 118 119 120 121 122 ...
  ..$ index  : num [1:13] -0.181 -0.676 0.32 0.126 0.523 ...
 $ :'data.frame':   13 obs. of  3 variables:
  ..$ months : int [1:13] 2 3 4 5 6 7 8 9 10 11 ...
  ..$ product: int [1:13] 114 115 116 117 118 119 120 121 122 123 ...
  ..$ index  : num [1:13] -0.6757 0.3198 0.1263 0.5232 -0.0099 ...
 $ :'data.frame':   13 obs. of  3 variables:
  ..$ months : int [1:13] 3 4 5 6 7 8 9 10 11 12 ...
  ..$ product: int [1:13] 115 116 117 118 119 120 121 122 123 124 ...
  ..$ index  : num [1:13] 0.3198 0.1263 0.5232 -0.0099 -1.0777 ...
 $ :'data.frame':   13 obs. of  3 variables:
  ..$ months : int [1:13] 4 5 6 7 8 9 10 11 12 13 ...
  ..$ product: int [1:13] 116 117 118 119 120 121 122 123 124 125 ...
  ..$ index  : num [1:13] 0.1263 0.5232 -0.0099 -1.0777 -0.7909 ...
 $ :'data.frame':   13 obs. of  3 variables:
  ..$ months : int [1:13] 5 6 7 8 9 10 11 12 13 14 ...
  ..$ product: int [1:13] 117 118 119 120 121 122 123 124 125 126 ...
  ..$ index  : num [1:13] 0.5232 -0.0099 -1.0777 -0.7909 1.4367 ...
 $ :'data.frame':   13 obs. of  3 variables:
  ..$ months : int [1:13] 6 7 8 9 10 11 12 13 14 15 ...
  ..$ product: int [1:13] 118 119 120 121 122 123 124 125 126 127 ...
  ..$ index  : num [1:13] -0.0099 -1.0777 -0.7909 1.4367 -1.1149 ...
 $ :'data.frame':   13 obs. of  3 variables:
  ..$ months : int [1:13] 7 8 9 10 11 12 13 14 15 16 ...
  ..$ product: int [1:13] 119 120 121 122 123 124 125 126 127 128 ...
  ..$ index  : num [1:13] -1.078 -0.791 1.437 -1.115 -0.671 ...
 $ :'data.frame':   13 obs. of  3 variables:
  ..$ months : int [1:13] 8 9 10 11 12 13 14 15 16 17 ...
  ..$ product: int [1:13] 120 121 122 123 124 125 126 127 128 129 ...
  ..$ index  : num [1:13] -0.791 1.437 -1.115 -0.671 -0.358 ...
 $ :'data.frame':   13 obs. of  3 variables:
  ..$ months : int [1:13] 9 10 11 12 13 14 15 16 17 18 ...
  ..$ product: int [1:13] 121 122 123 124 125 126 127 128 129 130 ...
  ..$ index  : num [1:13] 1.437 -1.115 -0.671 -0.358 -1.332 ...
 $ :'data.frame':   13 obs. of  3 variables:
  ..$ months : int [1:13] 10 11 12 13 14 15 16 17 18 19 ...
  ..$ product: int [1:13] 122 123 124 125 126 127 128 129 130 131 ...
  ..$ index  : num [1:13] -1.115 -0.671 -0.358 -1.332 0.622 ...
 $ :'data.frame':   13 obs. of  3 variables:
  ..$ months : int [1:13] 11 12 13 14 15 16 17 18 19 20 ...
  ..$ product: int [1:13] 123 124 125 126 127 128 129 130 131 132 ...
  ..$ index  : num [1:13] -0.671 -0.358 -1.332 0.622 -1.483 ...
 $ :'data.frame':   13 obs. of  3 variables:
  ..$ months : int [1:13] 12 13 14 15 16 17 18 19 20 21 ...
  ..$ product: int [1:13] 124 125 126 127 128 129 130 131 132 133 ...
  ..$ index  : num [1:13] -0.358 -1.332 0.622 -1.483 0.579 ...
 $ :'data.frame':   13 obs. of  3 variables:
  ..$ months : int [1:13] 13 14 15 16 17 18 19 20 21 22 ...
  ..$ product: int [1:13] 125 126 127 128 129 130 131 132 133 134 ...
  ..$ index  : num [1:13] -1.332 0.622 -1.483 0.579 -2.161 ...
 $ :'data.frame':   13 obs. of  3 variables:
  ..$ months : int [1:13] 14 15 16 17 18 19 20 21 22 23 ...
  ..$ product: int [1:13] 126 127 128 129 130 131 132 133 134 135 ...
  ..$ index  : num [1:13] 0.622 -1.483 0.579 -2.161 -0.544 ...
 $ :'data.frame':   13 obs. of  3 variables:
  ..$ months : int [1:13] 15 16 17 18 19 20 21 22 23 24 ...
  ..$ product: int [1:13] 127 128 129 130 131 132 133 134 135 136 ...
  ..$ index  : num [1:13] -1.483 0.579 -2.161 -0.544 -0.991 ...
 $ :'data.frame':   13 obs. of  3 variables:
  ..$ months : int [1:13] 16 17 18 19 20 21 22 23 24 25 ...
  ..$ product: int [1:13] 128 129 130 131 132 133 134 135 136 137 ...
  ..$ index  : num [1:13] 0.579 -2.161 -0.544 -0.991 0.351 ...
 $ :'data.frame':   13 obs. of  3 variables:
  ..$ months : int [1:13] 17 18 19 20 21 22 23 24 25 26 ...
  ..$ product: int [1:13] 129 130 131 132 133 134 135 136 137 138 ...
  ..$ index  : num [1:13] -2.161 -0.544 -0.991 0.351 0.801 ...
 $ :'data.frame':   13 obs. of  3 variables:
  ..$ months : int [1:13] 18 19 20 21 22 23 24 25 26 27 ...
  ..$ product: int [1:13] 130 131 132 133 134 135 136 137 138 139 ...
  ..$ index  : num [1:13] -0.544 -0.991 0.351 0.801 0.159 ...
 $ :'data.frame':   13 obs. of  3 variables:
  ..$ months : int [1:13] 19 20 21 22 23 24 25 26 27 28 ...
  ..$ product: int [1:13] 131 132 133 134 135 136 137 138 139 140 ...
  ..$ index  : num [1:13] -0.991 0.351 0.801 0.159 -0.479 ...
 $ :'data.frame':   13 obs. of  3 variables:
  ..$ months : int [1:13] 20 21 22 23 24 25 26 27 28 29 ...
  ..$ product: int [1:13] 132 133 134 135 136 137 138 139 140 141 ...
  ..$ index  : num [1:13] 0.351 0.801 0.159 -0.479 -0.581 ...
 $ :'data.frame':   13 obs. of  3 variables:
  ..$ months : int [1:13] 21 22 23 24 25 26 27 28 29 30 ...
  ..$ product: int [1:13] 133 134 135 136 137 138 139 140 141 142 ...
  ..$ index  : num [1:13] 0.801 0.159 -0.479 -0.581 0.276 ...
 $ :'data.frame':   13 obs. of  3 variables:
  ..$ months : int [1:13] 22 23 24 25 26 27 28 29 30 31 ...
  ..$ product: int [1:13] 134 135 136 137 138 139 140 141 142 143 ...
  ..$ index  : num [1:13] 0.159 -0.479 -0.581 0.276 -1.346 ...
 $ :'data.frame':   13 obs. of  3 variables:
  ..$ months : int [1:13] 23 24 25 26 27 28 29 30 31 32 ...
  ..$ product: int [1:13] 135 136 137 138 139 140 141 142 143 144 ...
  ..$ index  : num [1:13] -0.479 -0.581 0.276 -1.346 -0.861 ...


来源:https://stackoverflow.com/questions/42542586/create-a-list-of-a-list-of-dataframes-by-subsetting-a-list-of-dataframes-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!