Hi everyone, I am trying to calculate a particular variable (vector) from
some previously defined variables in a loop but I am having trouble figuring
out how to get the loop to recognize that it should index for the previously
defined objects. Here is a simplified version of what I am trying to do:
for(i in 1:10){
assign(paste("theta1_",i,sep=""),data.frame(scale(rnorm(250))))
assign(paste("theta2_",i,sep=""),data.frame(scale(rnorm(250))))
assign(paste("theta3_",i,sep=""),data.frame(theta1_i +
theta2_i)
}
I am having trouble with getting it to recognize that theta3_i should be
calculated using theta1_i and theta2_i in the third line. In other words,
theta3_1 should equal theta1_1+theta2_1 whereas theta3_2 should equal
theta1_2+theta2_2
Any advice as to where I am losing this one?
Thanks,
brendan
[[alternative HTML version deleted]]
On 4/20/2009 2:35 PM, Brendan Morse wrote:> Hi everyone, I am trying to calculate a particular variable (vector) from > some previously defined variables in a loop but I am having trouble figuring > out how to get the loop to recognize that it should index for the previously > defined objects. Here is a simplified version of what I am trying to do: > > for(i in 1:10){ > > assign(paste("theta1_",i,sep=""),data.frame(scale(rnorm(250)))) > > assign(paste("theta2_",i,sep=""),data.frame(scale(rnorm(250)))) > assign(paste("theta3_",i,sep=""),data.frame(theta1_i + > theta2_i) > } > > I am having trouble with getting it to recognize that theta3_i should be > calculated using theta1_i and theta2_i in the third line. In other words, > theta3_1 should equal theta1_1+theta2_1 whereas theta3_2 should equal > theta1_2+theta2_2 > > Any advice as to where I am losing this one?You have no variables theta1_i and theta2_i. You would have to construct the names and use get() to get the associated values. But why make your life so complicated? Why not just store everything in lists, indexed by i? E.g. theta1 <- list() theta2 <- list() theta3 <- list() for (i in 1:10) { theta1[[i]] <- data.frame(scale(rnorm(250))) etc. (You could even put everything in a single list of lists, but I don't think that makes it easier to read.) Duncan Murdoch
Brendan,
I think you should create objects outside of the "for" loop. You
can't
create objects instide the loop. You can try this:
metalist1<-list()
for(i in 1:10)
{metalist1[[i]]<-assign(paste("theta1_",i,sep=""),data.frame(scale(rnorm(250))))}
lapply(metalist1,function(x){print(dim(x))}) # Checking it out
metalist2<-list()
for(i in 1:10)
{metalist2[[i]]<-assign(paste("theta1_",i,sep=""),data.frame(scale(rnorm(250))))}
lapply(metalist1,function(x){print(dim(x))}) # Checking it out
metalist3<-list()
for(i in 1:10) {metalist3[[i]]<-metalist1[[i]]+metalist2[[i]]}
lapply(metalist3,function(x){print(dim(x))}) # Checking it out
# Checking
sum(metalist1[[1]]+metalist2[[1]])
sum(metalist3[[1]])
Dimitri
On Mon, Apr 20, 2009 at 2:35 PM, Brendan Morse <morse.brendan at
gmail.com> wrote:> Hi everyone, I am trying to calculate a particular variable (vector) from
> some previously defined variables in a loop but I am having trouble
figuring
> out how to get the loop to recognize that it should index for the
previously
> defined objects. Here is a simplified version of what I am trying to do:
>
> for(i in 1:10){
>
>
assign(paste("theta1_",i,sep=""),data.frame(scale(rnorm(250))))
>
>
assign(paste("theta2_",i,sep=""),data.frame(scale(rnorm(250))))
> ? ? ? ? ? ? ? ? ? ?
?assign(paste("theta3_",i,sep=""),data.frame(theta1_i +
> theta2_i)
> ? ? ? ? ? ? ? ? ? }
>
> I am having trouble with getting it to recognize that theta3_i should be
> calculated using theta1_i and theta2_i in the third line. In other words,
> theta3_1 should equal theta1_1+theta2_1 whereas theta3_2 should equal
> theta1_2+theta2_2
>
> Any advice as to where I am losing this one?
>
> Thanks,
> brendan
>
> ? ? ? ?[[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>
--
Dimitri Liakhovitski
MarketTools, Inc.
Dimitri.Liakhovitski at markettools.com
Hi Brendan,
If you really, really want to work with the for() loop, then
for(i in 1:10){
assign(paste("theta1_",i,sep=""),data.frame(scale(rnorm(250))))
assign(paste("theta2_",i,sep=""),data.frame(scale(rnorm(250))))
assign(paste("theta3_",i,sep=""),
get(paste("theta1_",i,sep=""))
+ get(paste("theta2_",i,sep="")))
}
should work. I would rather prefer to work with list() as Duncan Murdoch
and Dimitri Liakhovitski pointed out.
See ?get for more details.
HTH,
Jorge
On Mon, Apr 20, 2009 at 2:35 PM, Brendan Morse
<morse.brendan@gmail.com>wrote:
> Hi everyone, I am trying to calculate a particular variable (vector) from
> some previously defined variables in a loop but I am having trouble
> figuring
> out how to get the loop to recognize that it should index for the
> previously
> defined objects. Here is a simplified version of what I am trying to do:
>
> for(i in 1:10){
>
>
assign(paste("theta1_",i,sep=""),data.frame(scale(rnorm(250))))
>
>
assign(paste("theta2_",i,sep=""),data.frame(scale(rnorm(250))))
>
assign(paste("theta3_",i,sep=""),data.frame(theta1_i +
> theta2_i)
> }
>
> I am having trouble with getting it to recognize that theta3_i should be
> calculated using theta1_i and theta2_i in the third line. In other words,
> theta3_1 should equal theta1_1+theta2_1 whereas theta3_2 should equal
> theta1_2+theta2_2
>
> Any advice as to where I am losing this one?
>
> Thanks,
> brendan
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@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.
>
[[alternative HTML version deleted]]