# [R] creating expressions inside loop

Jan Goebel jgoebel at diw.de
Fri Apr 27 16:35:41 CEST 2001

Hello all,

may be someone can give me a hint how to
solve a little problem.

I'm trying to create some expressions inside
a for-loop which i want to evaluate AFTER this
creation loop (but loop specific).

To explain my problem here comes the code:

y.mat <- t(matrix(rep(84:90,8) + as.numeric(gl(8,7)),nrow=7))

# > y.mat[1:2,]
#      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
# [1,]   85   86   87   88   89   90   91
# [2,]   86   87   88   89   90   91   92

shift <- list()
for (i in 1:8) {
shift[[i]] <- expression(
eval(
parse(text=paste("cbind(",
paste("w1", y.mat[i,],sep="",collapse = ", "),")")
)
)
)
}
# which gives no error, but

> dimnames(eval(shift[[1]]))[2]
[[1]]
[1] "w189" "w190" "w191" "w192" "w193" "w194" "w195"
> dimnames(eval(shift[[4]]))[2]
[[1]]
[1] "w189" "w190" "w191" "w192" "w193" "w194" "w195"

is all the same, because "i" is fixed by the last value
in the loop. So how can i change

> shift[1]
[[1]]
expression(eval(parse(text = paste("cbind(", paste("w1", y.mat[i,],
sep = "", collapse = ", "), ")"))))

to an "in-loop evaluating" of
> paste("w1", y.mat[i,],sep="",collapse = ", ")
[1] "w189, w190, w191, w192, w193, w194, w195"

so that the expression looks like
> shift[1]
[[1]]
expression(eval(parse(text = paste("cbind(",
"w189, w190, w191, w192, w193, w194, w195", ")"))))

hope this is understandable ;-)

best

jan
--
+-----------------------------------
Jan Goebel (mailto:jgoebel at diw.de)

DIW Berlin
Longitudinal Data and Microanalysis
Königin-Luise-Str. 5
D-14195 Berlin -- Germany --
phone: 49 30 89789-377
+-----------------------------------
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._