Calling objects in a loop
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