DO NOT DO THIS! This is a complete misuse of R.
R is not a macro/scripting language that constructs language expressions to be evaluated (although it can do this). Rather, it is a functional language that uses functions that build new objects from inputted objects and parameters. I would strongly suggest reading An Introduction to R or other R tutorial (there are many on the web) and perhaps relevant portions of the R Language Definition manual to better understand what this means.You are more or less wasting your time with R if you do not understand and use its underlying programming paradigm. The practical consequence of this dictum is that one should almost NEVER use eval(parse()) -- instead construct a function that does what is needed. In the present example, instead of doing what you and Arun have described, do something like: addthings <- function(x,y,a) {x + y + a} ## the curly brackets are actually unnecessary aa <- within(aa, z <- addthings(x,y,-2) ) -- Bert On Wed, Sep 25, 2013 at 5:52 AM, arun <smartpink...@yahoo.com> wrote: > Also, Instead of ?attach, you could try ?with or ?within > aa$z<- with(aa,eval(parse(text=bb))) > aa$z > > #[1] 3 7 11 > > aa<- within(aa,z<- eval(parse(text=bb))) > aa > # x y z > #1 2 3 3 > #2 4 5 7 > #3 6 7 11 > A.K. > > > > ----- Original Message ----- > From: arun <smartpink...@yahoo.com> > To: R help <r-help@r-project.org> > Cc: > Sent: Wednesday, September 25, 2013 8:47 AM > Subject: Re: Re: Creating a new column to a data frame using a formula > from another variable > > Hi, > > Change: > aa$z<- eval(parse(text=bb)) > > aa > # x y z > #1 2 3 3 > #2 4 5 7 > #3 6 7 11 > A.K. > > I want to create a new column to a data frame using a formula from another > variable: > Example: > I have a data set "aa" is; > x y > 2 3 > 4 5 > 6 7 > > My R code is; > >bb <- "x+y-2" > >attach(aa) > >aa$z<- bb > >detach(aa) > > the result is; > x y z > 2 3 x+y-2 > 4 5 x+y-2 > 6 7 x+y-2 > > but I want like; > x y z > 2 3 3 > 4 5 7 > 6 7 11 > > Could you please help me.. > > ______________________________________________ > R-help@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. > -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm [[alternative HTML version deleted]] ______________________________________________ R-help@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.