creating and then executing command strings
Greg: Thanks for this concise explanation! I will have a look at the fortunes you mention. Best - P
On Fri, May 15, 2009 at 7:36 PM, Greg Snow <Greg.Snow at imail.org> wrote:
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). The code:
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). The code:
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 Intermountain Healthcare greg.snow at imail.org 801.408.8111
-----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='') ) )
or
text = paste("avg_",colname, " <- 0;", sep='')
parse(text)
eval(parse(text))
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 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.
Sharing Nicely at www.bokaap.net