[R] creating expressions inside loop

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Fri Apr 27 17:25:49 CEST 2001


Jan Goebel <jgoebel at diw.de> writes:

> 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

You could use

  substitute(...., list(i=i))

instead of 

  expression(....)

But wouldn't you rather do something like

f <- function(row) {
    n <- paste("w1",row, sep="")
    m <- do.call("cbind", lapply(n, get))
    names(m) <- n
    m
}

apply(y.mat, 1, f)

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list