[R] creating and then executing command strings
Greg.Snow at imail.org
Fri May 15 19:36:01 CEST 2009
The arrow "<-" is used to assign a value to a variable, the equals sign "=" is used to specify the value for a function argument. Recent versions of R allow "=" to be used for "<-" at the top level and certain circumstances which some people find more convenient, but can also lead to confusion (purists always keep them separate).
> parse( text <- paste( ...
Will take the results of paste, save them in a variable named text, then pass a copy to the first argument of parse, which is file, not text, so parse will just get confused (looking for a file named what your code is).
> parse( text = paste( ...
Will take the results of paste and pass them to the parse function as the text argument.
But having said that, you should refer to fortune(106) (type that after loading the fortunes package) and possibly fortune(181).
There are probably better ways to do what you want, Romain's second example is one way.
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
greg.snow at imail.org
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Philipp Schmidt
> Sent: Friday, May 15, 2009 8:35 AM
> To: Romain Francois
> Cc: r-help at r-project.org
> Subject: Re: [R] creating and then executing command strings
> On Fri, May 15, 2009 at 3:38 PM, Romain Francois
> <romain.francois at dbmail.com> wrote:
> > Hi,
> > You can either parse and eval the string you are making, as in:
> > eval( parse( text = paste("avg_",colname, " <- 0;", sep='') ) )
> > Or you can do something like this:
> > df[[ paste( "avg_", colname, sep = "" ) ]] <- 0
> Thanks you so much! I used the first version and it worked.
> What puzzles me, is that I am not able to use <- instead of = (my R
> book says the two can be exchanged) or break the command into
> different parts and execute them one after another.
> I get various error messages when I try:
> eval( parse( text <- paste("avg_",colname, " <- 0;", sep='') ) )
> text = paste("avg_",colname, " <- 0;", sep='')
> Anyway, thanks a lot - you greatly improved the likelihood of me not
> working on the weekend!
> Best - P
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help