[R] assignment to a symbol created by paste

Marc Schwartz (via MN) mschwartz at mn.rr.com
Thu Apr 13 22:43:56 CEST 2006


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. Bhatti


See 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' --




More information about the R-help mailing list