【发布时间】:2021-11-22 09:43:21
【问题描述】:
你好,我从来没有在这里写过,因为我总能找到我的问题的答案 但是现在我有一个,但我找不到解决它的方法。我有几个列表,其中包含列表。每个列表都是代码的一部分,我想粘贴所有这些列表以获得所有可能的组合。
这是一个列表示例
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 组合一个),每个内容都进行一次迭代。
我有 6 个 eval、3 个 cont、4 个 cont 和 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(Eval); contL = 1:length(cont); conL = 1:length(con); sceL = 1:length(sce); combos <-expand.grid(evalL, contL, conL, sceL); vapply(combos, function(Var1, Var2, Var3, Var4) list(evaluation=evaluation[Var1], Cont = cont[Var2], con = con[Var3], scenario = sce[Var4])stackoverflow.com/questions/5963269/…
标签: r list loops iteration combinations