Hello, I am creating a number of objects that I wish to have a common name with an index such as x1, x2, x3, ... I would like to do everyting in a loop to make the code compact and minimize the probability of an error by typo. A test problem may look like for (j in 1:10){ as.symbol(paste("x",j,sep="")) <- j; } which ideally would produce x1 = 1, ... x10 = 10. However, this does not work.> as.symbol(paste("x",1,sep="")) <- 2Error: Target of assignment expands to non-language object>Any help? Thanks, Chad R. Bhatti
On Thu, 2006-04-13 at 15:04 -0500, Chad Reyhan Bhatti wrote:> Hello, > > I am creating a number of objects that I wish to have a common name with > an index such as x1, x2, x3, ... I would like to do everyting in a loop to > make the code compact and minimize the probability of an error by typo. > A test problem may look like > > for (j in 1:10){ > as.symbol(paste("x",j,sep="")) <- j; > } > > which ideally would produce x1 = 1, ... x10 = 10. However, this does not > work. > > > as.symbol(paste("x",1,sep="")) <- 2 > Error: Target of assignment expands to non-language object > > > > Any help? > > Thanks, > > Chad R. BhattiSee the first example in ?assign HTH, Marc Schwartz P.S. To R Core: The comment in that example currently reads: #-- Create objects 'r1', 'r2', ... 'r6' -- It should be (note periods in vector names, as sep = "."): #-- Create objects 'r.1', 'r.2', ... 'r.6' --
On Thu, 13 Apr 2006, Chad Reyhan Bhatti wrote:> Hello, > > I am creating a number of objects that I wish to have a common name with > an index such as x1, x2, x3, ... I would like to do everyting in a loop to > make the code compact and minimize the probability of an error by typo.see FAQ 7.21 (including the suggestion not to do this). -thomas> A test problem may look like > > for (j in 1:10){ > as.symbol(paste("x",j,sep="")) <- j; > } > > which ideally would produce x1 = 1, ... x10 = 10. However, this does not > work. > >> as.symbol(paste("x",1,sep="")) <- 2 > Error: Target of assignment expands to non-language object >> > > Any help? > > Thanks, > > Chad R. Bhatti > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >Thomas Lumley Assoc. Professor, Biostatistics tlumley at u.washington.edu University of Washington, Seattle