遍历多个列表以获取 R 中的每个迭代
Loop through multiple lists to get every iterations in R
你好我从来没有在这里写过因为我总能找到我的问题的答案
但是现在我有一个,我找不到解决它的方法。我有几个列表,其中包含列表。每个列表都是代码的一部分,我想粘贴所有这些列表以获得所有可能的组合。
这是一个列表示例
evaluation <- list()
for(i in 1:length(Dates)){
evaluation[[i]] <- list(snapshotDate = toString(Dates[i]))
}
Here is a picture of what is in the evaluation list
这是我要“粘贴”所有列表的最终内容。我在这里为每个选择第一个列表。
content <- list(Eval = evaluation[1],
cont = cont[1],
con = con[1],
scenario = sce)
最后的目标是有很多这样的内容(Eval、cont、con 和 sce 的每个组合一个),每个内容都有一个迭代。
One content looks like this
我有 6 个 eval、3 个 cont、4 个 con 和 1 个 sce(但它们会随着时间的推移而变化,因此这部分的代码应该是通用的)而且我不知道如何编写代码。我尝试了一个循环,但无法获得所有组合。谁能帮帮我。
感谢阅读我的文章,希望我能得到一些答案
编辑:这是@Skaqqs
要求的我的代码
library(parsedate)# to have date in ISO8601
Days = 4
CL_list = c(0.99,0.95,0.90)
measuretype = c("relative")
TimeH = c(10,30,252)
PTF = c("1")
# Prepare Table of evaluations according to number of Days selected
Dates = lst(format_iso_8601(format(Sys.time())))
for (i in 0:Days){
tmp <- format_iso_8601(format(Sys.Date() - i))
Dates <- rbind(Dates, tmp)
}
# create Evaluation
evaluation <- list()
for( i in 1:length(Dates)){
evaluation[[i]] <- list(snapshotDate = toString(Dates[i]))
}
# create Cont
cont <- list()
for( i in 1:length(CL_list)){
cont[[i]] <- list(measureType = measuretype[1], confidenceLevel = CL_list[i])
}
# create con
con <- list()
for(i in 1:length(TimeH)){
con[[i]] <- list(type = 'connect', timeHorizon = TimeH[i])
}
# create sce
sce <- list(currency = 'USD', amountScheme = 'quantity', positions = "pos")
content <- list(Eval = evaluation[1],
cont = cont[1],
con = con[1],
scenario = sce)
使用 @Skaqqs 的 2 个屏幕截图进行编辑
How results should look like
How it looks like
你可以试试这个。示例数据如下
evalL = 1:length(evaluation)
contL = 1:length(cont)
conL = 1:length(con)
sceL = 1:length(sce)
combos <- expand.grid(evalL, contL, conL, sceL)
results <- lapply(1:nrow(combos), FUN = function(x)
list(evaluation = evaluation[[combos[x,"Var1"]]], cont = cont[[combos[x,"Var2"]]], con = con[[combos[x,"Var3"]]], scenario = sce[[combos[x,"Var4"]]]))
str(results[1:4])
#> List of 4
#> $ :List of 4
#> ..$ evaluation: int 1
#> ..$ cont : chr "Jan"
#> ..$ con : chr "a"
#> ..$ scenario : chr "scenario X"
#> $ :List of 4
#> ..$ evaluation: int 2
#> ..$ cont : chr "Jan"
#> ..$ con : chr "a"
#> ..$ scenario : chr "scenario X"
#> $ :List of 4
#> ..$ evaluation: int 3
#> ..$ cont : chr "Jan"
#> ..$ con : chr "a"
#> ..$ scenario : chr "scenario X"
#> $ :List of 4
#> ..$ evaluation: int 4
#> ..$ cont : chr "Jan"
#> ..$ con : chr "a"
#> ..$ scenario : chr "scenario X"
数据:
evaluation <- list()
for(i in 1:6){
evaluation[i] <- list(i)
}
mos <- c("Jan", "Feb", "Mar")
cont <- list()
for(i in 1:3){
cont[i] <- list(mos[i])
}
con <- list()
for(i in 1:4){
con[i] <- list(letters[i])
}
sce <- list("scenario X")
由 reprex 包 (v2.0.1) 创建于 2021-09-30
编辑:
library(parsedate)# to have date in ISO8601
library(dplyr)
Days = 4
CL_list = c(0.99,0.95,0.90)
measuretype = c("relative")
TimeH = c(10,30,252)
PTF = c("1")
# Prepare Table of evaluations according to number of Days selected
Dates = lst(format_iso_8601(format(Sys.time())))
for (i in 0:Days){
tmp <- format_iso_8601(format(Sys.Date() - i))
Dates <- rbind(Dates, tmp)
}
# create Evaluation
evaluation <- list()
for( i in 1:length(Dates)){
evaluation[[i]] <- list(snapshotDate = toString(Dates[i]))
}
# create Cont
cont <- list()
for( i in 1:length(CL_list)){
cont[[i]] <- list(measureType = measuretype[1], confidenceLevel = CL_list[i])
}
# create con
con <- list()
for(i in 1:length(TimeH)){
con[[i]] <- list(type = 'connect', timeHorizon = TimeH[i])
}
# create sce
sce <- list(currency = 'USD', amountScheme = 'quantity', positions = "pos")
evalL = 1:length(evaluation)
contL = 1:length(cont)
conL = 1:length(con)
sceL = 1:length(sce)
combos <- expand.grid(evalL, contL, conL, sceL)
evalList <- lapply(combos$Var1, function(x) evaluation[[x]])
contList <- lapply(combos$Var2, function(x) cont[[x]])
conList <- lapply(combos$Var3, function(x) con[[x]])
sceList <- lapply(combos$Var4, function(x) sce[[x]])
content <- list(evaluation = evalList, cont = contList, con = conList, scenario = sceList)
由 reprex 包 (v2.0.1) 创建于 2021-09-30
你好我从来没有在这里写过因为我总能找到我的问题的答案 但是现在我有一个,我找不到解决它的方法。我有几个列表,其中包含列表。每个列表都是代码的一部分,我想粘贴所有这些列表以获得所有可能的组合。
这是一个列表示例
evaluation <- list()
for(i in 1:length(Dates)){
evaluation[[i]] <- list(snapshotDate = toString(Dates[i]))
}
Here is a picture of what is in the evaluation list
这是我要“粘贴”所有列表的最终内容。我在这里为每个选择第一个列表。
content <- list(Eval = evaluation[1],
cont = cont[1],
con = con[1],
scenario = sce)
最后的目标是有很多这样的内容(Eval、cont、con 和 sce 的每个组合一个),每个内容都有一个迭代。
One content looks like this
我有 6 个 eval、3 个 cont、4 个 con 和 1 个 sce(但它们会随着时间的推移而变化,因此这部分的代码应该是通用的)而且我不知道如何编写代码。我尝试了一个循环,但无法获得所有组合。谁能帮帮我。
感谢阅读我的文章,希望我能得到一些答案
编辑:这是@Skaqqs
要求的我的代码library(parsedate)# to have date in ISO8601
Days = 4
CL_list = c(0.99,0.95,0.90)
measuretype = c("relative")
TimeH = c(10,30,252)
PTF = c("1")
# Prepare Table of evaluations according to number of Days selected
Dates = lst(format_iso_8601(format(Sys.time())))
for (i in 0:Days){
tmp <- format_iso_8601(format(Sys.Date() - i))
Dates <- rbind(Dates, tmp)
}
# create Evaluation
evaluation <- list()
for( i in 1:length(Dates)){
evaluation[[i]] <- list(snapshotDate = toString(Dates[i]))
}
# create Cont
cont <- list()
for( i in 1:length(CL_list)){
cont[[i]] <- list(measureType = measuretype[1], confidenceLevel = CL_list[i])
}
# create con
con <- list()
for(i in 1:length(TimeH)){
con[[i]] <- list(type = 'connect', timeHorizon = TimeH[i])
}
# create sce
sce <- list(currency = 'USD', amountScheme = 'quantity', positions = "pos")
content <- list(Eval = evaluation[1],
cont = cont[1],
con = con[1],
scenario = sce)
使用 @Skaqqs 的 2 个屏幕截图进行编辑 How results should look like How it looks like
你可以试试这个。示例数据如下
evalL = 1:length(evaluation)
contL = 1:length(cont)
conL = 1:length(con)
sceL = 1:length(sce)
combos <- expand.grid(evalL, contL, conL, sceL)
results <- lapply(1:nrow(combos), FUN = function(x)
list(evaluation = evaluation[[combos[x,"Var1"]]], cont = cont[[combos[x,"Var2"]]], con = con[[combos[x,"Var3"]]], scenario = sce[[combos[x,"Var4"]]]))
str(results[1:4])
#> List of 4
#> $ :List of 4
#> ..$ evaluation: int 1
#> ..$ cont : chr "Jan"
#> ..$ con : chr "a"
#> ..$ scenario : chr "scenario X"
#> $ :List of 4
#> ..$ evaluation: int 2
#> ..$ cont : chr "Jan"
#> ..$ con : chr "a"
#> ..$ scenario : chr "scenario X"
#> $ :List of 4
#> ..$ evaluation: int 3
#> ..$ cont : chr "Jan"
#> ..$ con : chr "a"
#> ..$ scenario : chr "scenario X"
#> $ :List of 4
#> ..$ evaluation: int 4
#> ..$ cont : chr "Jan"
#> ..$ con : chr "a"
#> ..$ scenario : chr "scenario X"
数据:
evaluation <- list()
for(i in 1:6){
evaluation[i] <- list(i)
}
mos <- c("Jan", "Feb", "Mar")
cont <- list()
for(i in 1:3){
cont[i] <- list(mos[i])
}
con <- list()
for(i in 1:4){
con[i] <- list(letters[i])
}
sce <- list("scenario X")
由 reprex 包 (v2.0.1) 创建于 2021-09-30
编辑:
library(parsedate)# to have date in ISO8601
library(dplyr)
Days = 4
CL_list = c(0.99,0.95,0.90)
measuretype = c("relative")
TimeH = c(10,30,252)
PTF = c("1")
# Prepare Table of evaluations according to number of Days selected
Dates = lst(format_iso_8601(format(Sys.time())))
for (i in 0:Days){
tmp <- format_iso_8601(format(Sys.Date() - i))
Dates <- rbind(Dates, tmp)
}
# create Evaluation
evaluation <- list()
for( i in 1:length(Dates)){
evaluation[[i]] <- list(snapshotDate = toString(Dates[i]))
}
# create Cont
cont <- list()
for( i in 1:length(CL_list)){
cont[[i]] <- list(measureType = measuretype[1], confidenceLevel = CL_list[i])
}
# create con
con <- list()
for(i in 1:length(TimeH)){
con[[i]] <- list(type = 'connect', timeHorizon = TimeH[i])
}
# create sce
sce <- list(currency = 'USD', amountScheme = 'quantity', positions = "pos")
evalL = 1:length(evaluation)
contL = 1:length(cont)
conL = 1:length(con)
sceL = 1:length(sce)
combos <- expand.grid(evalL, contL, conL, sceL)
evalList <- lapply(combos$Var1, function(x) evaluation[[x]])
contList <- lapply(combos$Var2, function(x) cont[[x]])
conList <- lapply(combos$Var3, function(x) con[[x]])
sceList <- lapply(combos$Var4, function(x) sce[[x]])
content <- list(evaluation = evalList, cont = contList, con = conList, scenario = sceList)
由 reprex 包 (v2.0.1) 创建于 2021-09-30