我有一个列表,如下所示
str(list_ts_split)
List of 5
$ date :List of 2
..$ train: Time-Series [1:24] from 2019 to 2021: 1.55e+09 1.55e+09 1.55e+09 1.55e+09
1.56e+09 ...
..$ test : Time-Series [1:6] from 2021 to 2021: 1.61e+09 1.61e+09 1.61e+09 1.62e+09 1.62e+09
...
$ actualB1:List of 2
..$ train: Time-Series [1:24] from 2019 to 2021: 5463 7618 3745 6760 5856 ...
..$ test : Time-Series [1:6] from 2021 to 2021: 5535 7326 6195 2435 3041 ...
$ actualB2:List of 2
..$ train: Time-Series [1:24] from 2019 to 2021: 6523 1734 9544 4687 8076 ...
..$ test : Time-Series [1:6] from 2021 to 2021: 3647 9272 4974 5931 1459 ...
$ actualAx:List of 2
..$ train: Time-Series [1:24] from 2019 to 2021: 193 200 310 149 719 357 470 623 678 533 ...
..$ test : Time-Series [1:6] from 2021 to 2021: 274 142 968 831 178 184
$ actualAy:List of 2
..$ train: Time-Series [1:24] from 2019 to 2021: 3053 4351 3284 2155 1805 ...
..$ test : Time-Series [1:6] from 2021 to 2021: 8236 1585 2324 5692 4249 ...
我可以使用下面的代码访问列表列表的元素
df1_tstrain <- list_ts_split$actualB1$train
df1_tstest <- list_ts_split$actualB1$test
list_ts_split $actualB1$train
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2019 5463 7618 3745 6760 5856 2645 4061 1162 6829 7584 8383 4016
2020 2827 1666 3753 2004 1757 9360 5989 9031 1584 1435 8365 9209
list_ts_split$actualB1$test
Jan Feb Mar Apr May Jun
2021 5535 7326 6195 2435 3041 2737
EDIT1:我的问题是,有没有办法创建一个循环或使用一个函数来做与df1_tstrain <- ActialB1$train一样的事情,而不需要我在全局环境中将值硬编码为变量名??
EDIT2:
我现在正在尝试下面这样的东西
for (i in 1:length(list_ts_split)){
assign(paste0("tstrain",i}.as.data.frame(list_ts_split[[i]]))
}
发布于 2021-06-25 06:48:02
您没有一个有效的示例,所以我不确定我是否理解您的问题。话虽如此,这里有一些可以尝试的东西。
## Some data to work with
lst <- list(
date = list(
train = 2000:2010,
test = 2011:2021
),
actualB1 = list(
train = 2000:2010,
test = 2011:2021
),
actualAx = list(
train = 2000:2010,
test = 2011:2021
)
)
## A function to subset the list at levels 1 and 2.
f <- function(lst, level_1, level_2){
# l1 <- lst[[level_1]]
# l1[[level_2]]
l1 <- sapply(X = lst, FUN = "[", level_2, simplify = FALSE)
l1[[level_1]]
}
## testing the function on the list specifying the names of the 2 levels
test <- f(lst = lst, level_1 = 'actualB1', level_2 = 'train')
test
我仍然不确定我是否理解了这个问题,但这是我在递归函数中提出的另一个想法。
## Data
lst <- list(
date = data.frame(
train = 2000:2005,
test = 2005:2010
),
actualB1 = data.frame(
train = 2010:2015,
test = 2015:2020
),
actualAx = data.frame(
train = 2020:2025,
test = 2030:2035
)
)
# Function
f <- function(lst){
names_lst <- names(lst)
lapply(X = names_lst, function(i){
if(inherits(lst[[i]], what = 'data.frame', which = TRUE)){
names_i <- names(lst[[i]])
lapply(X = names_i, function(j){
obj_name <- paste(i, j, sep = "_")
assign(obj_name, lst[[i]][[j]], envir = .GlobalEnv)
})
} else {
f (lst)
}
})
}
# Function called on Data
test <- f(lst = lst)
我不确定是否理解你的问题的原因是,我似乎不明白为什么有人想要保持一个列表的子集,并将结果分配给新的对象。
我认为,如果你想对你正在子集的数据进行进一步的分析,你需要重新考虑你的方法。虽然上面的函数通常会检索列表中不同嵌套级别的名称,但它可能会遇到错误C stack usage <number> is too close to the limit
,这取决于您的设置。这可能是一个信号,表明你可能需要重新考虑你的方法。尝试向lst
添加更多级别的嵌套,最终可能会遇到这种错误。在我的机器上,以下代码导致了这个错误:
lst <- list(
date = data.frame(
train = 2000:2005,
test = 2005:2010
),
actualB1 = data.frame(
train = 2010:2015,
test = 2015:2020
),
actualAx = list(
up = data.frame(
train = 2020:2025,
test = 2030:2035
),
sub = data.frame (
train = 2040:2045,
test = 2045:2050
)
)
)
test <- f(lst = lst) # Error: C stack usage 15926032 is too close to the limit
https://stackoverflow.com/questions/68123150
复制相似问题