[R] ls() pattern

Richard.Cotton at hsl.gov.uk Richard.Cotton at hsl.gov.uk
Thu Dec 13 19:35:11 CET 2007


>   Yes, I know this works. I just give it for example to show what I 
> would like to achieve. In fact, I have many variables, you can 
> imagine it is very annoying to construct the data frame by mannually
> assigning the variables and binding them. Therefore, I would like to
> do it automaticlly. So it is back to my first two questions, In 
> order to achieve cbcname1 = 0.1, I tried sth like:(but it does not 
> return the result as desired)
> paste("cbc","name1","=",0.1,sep=""), in this way, I can construct 
> all the variables by a single step.Then I would like to build the 
> data frame automaticlly binding all the "cbcname=.." together. Just 
> like the example.

Ok.  How's this?

#Generate variable names
varnames = paste("cbcname", 1:10, sep="")
#Assign them to values
for(i in 1:10) assign(varnames[i], 0.1*i)
#Form the middle bit of the command
cmd = paste(varnames, "=", mget(varnames, envir=.GlobalEnv), collapse=",")
#Evaluate the command
eval(parse(text=paste("df=data.frame(", cmd, ")", sep="")))

There's a general rule that says if you have to use parse, you're probably 
doing it the wrong way, but it's been a long day, so that's the best way I 
can think of right now.

Regards,
Richie.

Mathematical Sciences Unit
HSL


------------------------------------------------------------------------
ATTENTION:

This message contains privileged and confidential inform...{{dropped:20}}



More information about the R-help mailing list