[R] Applying and labeling scenarios

Keith S Weintraub kw1958 at gmail.com
Fri Oct 4 20:38:21 CEST 2013


Folks,

I have a working version of the code below for my "real world" problem. I was wondering if there was a better way to do this.

I have 3 sets of parameters and I want to iterate over all combinations and label the results.

Would some of the plyr tools make this easier?

# Input Scenarios
scens<-list(A=c(.2,.3), B= c(.2,.4), C=2:3)

# All combinations
allPerms<-expand.grid(scens)

# Create labels
labels<-sapply(1:nrow(allPerms), 
              function(x, y) paste0("[", paste0(y[x,], collapse = "|"), "]"), 
              allPerms)

# Simple example computation
vals<-sapply(1:nrow(allPerms), function(x, y) cumsum(t(y[x,])), allPerms)

# Apply labels to columns
colnames(vals)<-labels

# "Beautiful" output!
dput(vals)
structure(c(0.2, 0.4, 2.4, 0.3, 0.5, 2.5, 0.2, 0.6, 2.6, 0.3, 
0.7, 2.7, 0.2, 0.4, 3.4, 0.3, 0.5, 3.5, 0.2, 0.6, 3.6, 0.3, 0.7, 
3.7), .Dim = c(3L, 8L), .Dimnames = list(NULL, c("[0.2|0.2|2]", 
"[0.3|0.2|2]", "[0.2|0.4|2]", "[0.3|0.4|2]", "[0.2|0.2|3]", "[0.3|0.2|3]", 
"[0.2|0.4|3]", "[0.3|0.4|3]")))


Thanks for your time,
KW

--



More information about the R-help mailing list