?labels1<-
paste0("[",with(allPerms,paste(A,B,C,sep="|")),"]")
vals2<-as.matrix(cumsum(as.data.frame(t(allPerms))))
?dimnames(vals2)<- list(NULL,labels1)
?all.equal(vals,vals2)
#[1] TRUE
A.K.
----- Original Message -----
From: Keith S Weintraub <kw1958 at gmail.com>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc:
Sent: Friday, October 4, 2013 2:38 PM
Subject: [R] Applying and labeling scenarios
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
--
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.