Re: [R] ls() pattern

From: <Richard.Cotton_at_hsl.gov.uk>
Date: Thu, 13 Dec 2007 18:35:11 +0000


> 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}}

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. Received on Thu 13 Dec 2007 - 18:39:49 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 13 Dec 2007 - 19:30:31 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.