我有一个包含6个数据帧的列表,我想创建一个包含24个数据帧的6个列表的列表,这24个数据帧是原始6个数据帧的子集。
以下是我正在尝试做的一个较短的例子:
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 })
当我运行它时,它会创建一个新的列表regdflist,它与dflist相同,并且不会创建新的子集数据帧。
我正在尝试在lapply函数中使用lapply函数,对数据进行子集化,并在原始列表中创建一个新列表。这可能吗?
有人能告诉我这里出错的地方吗?
这是我之前的问题,我将subfun
函数仅应用于一个数据框。我现在正在尝试将其用于多个数据帧。
Making a list of dataframes which are a subset of one dataframe using R
我也看到了这个答案: R create new list of dataframes of a list of dataframes 但我认为他所做的与我试图做的相似,我无法使其发挥作用。
非常感谢你的帮助
答案 0 :(得分: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 ...