Hello I have never written in here because I always found an answer to my problems But now I have one and I can't find a way to resolve it. I have several lists with list in them. Every list is a part of a code and I'd like to paste all these lists in order to obtain all the possible combinations.
here is a list example
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
And here is the final content where i want to "paste" all the list. Where here I choose the 1st list for each.
content <- list(Eval = evaluation[1],
cont = cont[1],
con = con[1],
scenario = sce)
The goal at the end is to have a lot of this content(one for each combination of Eval, cont,con and sce) with in each of them one iteration.
I have 6 eval, 3 cont, 4 con and 1 sce (but they change over time so the code should be generic for this part) and I don't know how i could code this. I tried a loop but I'am unable to get all the combinations. Could someone help me.
Thanks for reading me and I hope I'll get some answers
Edit: Here is my code as asked by @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)
Edit with the 2 screenshots for @Skaqqs How results should look like How it looks like
CodePudding user response:
You could try this. Example data is below
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"
Data:
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")
Created on 2021-09-30 by the reprex package (v2.0.1)
Edit:
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)
Created on 2021-09-30 by the reprex package (v2.0.1)