【问题标题】:Loop through multiple lists to get every iterations in R遍历多个列表以获取 R 中的每个迭代
【发布时间】: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 组合一个),每个内容都进行一次迭代。

One content looks like this

我有 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 &lt;-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


【解决方案1】:

你可以试试这个。示例数据如下

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 创建

【讨论】:

  • 不幸的是,它们在结果中的形状与内容中的形状不同。我在问题的末尾上传了 2 个屏幕截图,以便您查看问题所在。你知道怎么解决吗?
  • 感谢分享数据。我使用您发布的数据更新了我的答案。这是您希望的结构吗?
猜你喜欢
  • 2023-03-25
  • 1970-01-01
  • 2022-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-21
  • 2015-04-19
相关资源
最近更新 更多