[R] trivial question about nested loops

Duncan Murdoch murdoch at stats.uwo.ca
Thu Sep 2 19:20:06 CEST 2004


On Thu, 2 Sep 2004 09:52:07 -0700 (PDT), dan roberts
<bro092 at yahoo.com> wrote:

>Duncan,
>
>I tried your solutions and they almost work. The problem seems
>to be d$Y[k]. When I run the code with d$Y[k], I get an empty
>matrix (all 0s). However, if I replace d$Y[k] with, for example,
>d&Y4, then I get a matrix with one of the results I am looking
>for. Do you have any idea how I could make d$Y[k] actually pull
>the columns from the imported table (d$Y1, d$Y2, ...)?

What is d?  If it's a data.frame or other list, then 

d[[paste("Y",k,sep="")]] would get the Yk column.  If it's a matrix
with named columns

d[,paste("Y",k,sep="")]

would work.

>I tried
>sum(paste("d$Y",k,sep="")*exp(-1i*j*d$X)) 
>but I got 
>Error in paste("d$Y", k, sep = "") * exp(-(0+1i) * j * d$X) : 
>        non-numeric argument to binary operator

It thought you were trying to multiply by the string "d$Y1".  There
are a number of ways to evaluate a string as if it was code, but you
don't really need those.

Duncan Murdoch




More information about the R-help mailing list