Reputation: 113
I have output from a model written to a multilevel list. Essentially, there are 5 variables, and the list is constructed so that results to each scenario are identified by the variable values in that scenario. So the results of a run with K=0.5, C=0.9, V=0.5, R=0.25 and RV = 0 can be accessed by
results$`K=0.05`$`C=0.9`$`V=0.5`$`R=0.25`$`RV=0`
1] FALSE FALSE FALSE FALSE FALSE
Below is an example of how the full output might look:
results
$`K=0.05`
$`K=0.05`$`C=0.9`
$`K=0.05`$`C=0.9`$`V=0.5`
$`K=0.05`$`C=0.9`$`V=0.5`$`R=0.25`
$`K=0.05`$`C=0.9`$`V=0.5`$`R=0.25`$`RV=0`
[1] FALSE FALSE FALSE FALSE FALSE
$`K=0.05`$`C=0.9`$`V=0.5`$`R=0.25`$`RV=0.05`
[1] FALSE FALSE FALSE FALSE FALSE
$`K=0.05`$`C=0.9`$`V=0.5`$`R=0.25`$`RV=0.1`
[1] FALSE FALSE FALSE FALSE FALSE
$`K=0.05`$`C=0.91`
$`K=0.05`$`C=0.91`$`V=0.5`
$`K=0.05`$`C=0.91`$`V=0.5`$`R=0.25`
$`K=0.05`$`C=0.91`$`V=0.5`$`R=0.25`$`RV=0`
[1] FALSE FALSE FALSE FALSE FALSE
$`K=0.05`$`C=0.91`$`V=0.5`$`R=0.25`$`RV=0.05`
[1] FALSE FALSE FALSE FALSE FALSE
$`K=0.05`$`C=0.91`$`V=0.5`$`R=0.25`$`RV=0.1`
[1] FALSE FALSE FALSE FALSE FALSE
$`K=0.05`$`C=0.92`
$`K=0.05`$`C=0.92`$`V=0.5`
$`K=0.05`$`C=0.92`$`V=0.5`$`R=0.25`
$`K=0.05`$`C=0.92`$`V=0.5`$`R=0.25`$`RV=0`
[1] FALSE FALSE FALSE FALSE FALSE
$`K=0.05`$`C=0.92`$`V=0.5`$`R=0.25`$`RV=0.05`
[1] FALSE FALSE FALSE FALSE FALSE
$`K=0.05`$`C=0.92`$`V=0.5`$`R=0.25`$`RV=0.1`
[1] FALSE FALSE FALSE FALSE FALSE
$`K=0.1`
$`K=0.1`$`C=0.9`
$`K=0.1`$`C=0.9`$`V=0.5`
$`K=0.1`$`C=0.9`$`V=0.5`$`R=0.25`
$`K=0.1`$`C=0.9`$`V=0.5`$`R=0.25`$`RV=0`
[1] FALSE FALSE FALSE FALSE FALSE
$`K=0.1`$`C=0.9`$`V=0.5`$`R=0.25`$`RV=0.05`
[1] FALSE FALSE FALSE FALSE FALSE
$`K=0.1`$`C=0.9`$`V=0.5`$`R=0.25`$`RV=0.1`
[1] FALSE FALSE FALSE FALSE FALSE
$`K=0.1`$`C=0.91`
$`K=0.1`$`C=0.91`$`V=0.5`
$`K=0.1`$`C=0.91`$`V=0.5`$`R=0.25`
$`K=0.1`$`C=0.91`$`V=0.5`$`R=0.25`$`RV=0`
[1] FALSE FALSE FALSE FALSE FALSE
$`K=0.1`$`C=0.91`$`V=0.5`$`R=0.25`$`RV=0.05`
[1] FALSE FALSE FALSE FALSE FALSE
$`K=0.1`$`C=0.91`$`V=0.5`$`R=0.25`$`RV=0.1`
[1] FALSE FALSE FALSE FALSE FALSE
$`K=0.1`$`C=0.92`
$`K=0.1`$`C=0.92`$`V=0.5`
$`K=0.1`$`C=0.92`$`V=0.5`$`R=0.25`
$`K=0.1`$`C=0.92`$`V=0.5`$`R=0.25`$`RV=0`
[1] FALSE FALSE FALSE FALSE FALSE
$`K=0.1`$`C=0.92`$`V=0.5`$`R=0.25`$`RV=0.05`
[1] FALSE FALSE FALSE FALSE FALSE
$`K=0.1`$`C=0.92`$`V=0.5`$`R=0.25`$`RV=0.1`
[1] FALSE FALSE FALSE FALSE FALSE
$`K=0.15`
$`K=0.15`$`C=0.9`
$`K=0.15`$`C=0.9`$`V=0.5`
$`K=0.15`$`C=0.9`$`V=0.5`$`R=0.25`
$`K=0.15`$`C=0.9`$`V=0.5`$`R=0.25`$`RV=0`
[1] FALSE FALSE FALSE FALSE FALSE
$`K=0.15`$`C=0.9`$`V=0.5`$`R=0.25`$`RV=0.05`
[1] FALSE TRUE FALSE FALSE FALSE
$`K=0.15`$`C=0.9`$`V=0.5`$`R=0.25`$`RV=0.1`
[1] FALSE FALSE FALSE FALSE FALSE
$`K=0.15`$`C=0.91`
$`K=0.15`$`C=0.91`$`V=0.5`
$`K=0.15`$`C=0.91`$`V=0.5`$`R=0.25`
$`K=0.15`$`C=0.91`$`V=0.5`$`R=0.25`$`RV=0`
[1] FALSE FALSE FALSE FALSE FALSE
$`K=0.15`$`C=0.91`$`V=0.5`$`R=0.25`$`RV=0.05`
[1] FALSE FALSE FALSE FALSE FALSE
$`K=0.15`$`C=0.91`$`V=0.5`$`R=0.25`$`RV=0.1`
[1] TRUE FALSE FALSE FALSE FALSE
$`K=0.15`$`C=0.92`
$`K=0.15`$`C=0.92`$`V=0.5`
$`K=0.15`$`C=0.92`$`V=0.5`$`R=0.25`
$`K=0.15`$`C=0.92`$`V=0.5`$`R=0.25`$`RV=0`
[1] FALSE FALSE TRUE FALSE FALSE
$`K=0.15`$`C=0.92`$`V=0.5`$`R=0.25`$`RV=0.05`
[1] FALSE FALSE FALSE FALSE FALSE
$`K=0.15`$`C=0.92`$`V=0.5`$`R=0.25`$`RV=0.1`
[1] FALSE FALSE FALSE FALSE FALSE
For analysis, I'd like to be able extract results from all scenarios with specific variable values. For example, I'd like to extract the results of scenarios with C = 0.92 and RV = 0.1 (or any combination, really).
Something like:
results$*$'C=0.92'$*$*$'RV=0.1'
or
results[*]['C=0.92'][*][*]['RV=0.1']
I read a similar question regarding wildcards, which I don't think R utilizes, that didn't quite get where I needed it to go. Any suggestions?
Cheers!
Upvotes: 0
Views: 1180
Reputation: 52677
You really should be storing your data in a data frame or some such. I understand that the recursive structure is convenient when you're running tests in loops, but still. Fortunately there is a function that can do the translation:
lst <- list(a=list(b=list(c0=list(d0=1:3, e0=4:6), c1=list(d1=7:9, e1=10:12))))
library(reshape2)
DF <- melt(lst)
subset(DF, grepl("1", L4)) # now we can use regular expressions against each list level
Produces:
value L4 L3 L2 L1
7 7 d1 c1 b a
8 8 d1 c1 b a
9 9 d1 c1 b a
10 10 e1 c1 b a
11 11 e1 c1 b a
12 12 e1 c1 b a
You may want to look at DF
to see what I'm doing with the subset.
Upvotes: 3