Re: [R] 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.s...@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.s...@imail.org 801.408.8111 -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r- project.org] On Behalf Of Philipp Schmidt Sent: Friday, May 15, 2009 8:35 AM To: Romain Francois Cc: r-help@r-project.org Subject: Re: [R] creating and then executing command strings On Fri, May 15, 2009 at 3:38 PM, Romain Francois romain.franc...@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@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 __ 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.
[R] creating and then executing command strings
Hi: I very recently started experimenting with R and am occasionally running into very basic problems that I can't seem to solve. If there is an R-newbies forum that is more appropriate for these kinds of questions, please direct me to it. I'd like to automatically add vectors to a dataframe. I am able to build command strings that would do what I want, but R is not executing them. A simplified example: # Add three vectors called avg_col1, avg_col2, avg_col3 to dataframe df for(colname in c(col1, col2, col3)){ print(paste(df$avg_,colname, - 0;, sep='')) # Just using this to make sure the command is correct paste(avg_,colname, - 0;, sep='') # Does nothing } Output: [1] df$avg_col1 - 0; [1] df$avg_col2 - 0; [1] df$avg_col3 - 0; Thanks for your help! Best - P __ 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.
Re: [R] creating and then executing command strings
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 Romain Philipp Schmidt wrote: Hi: I very recently started experimenting with R and am occasionally running into very basic problems that I can't seem to solve. If there is an R-newbies forum that is more appropriate for these kinds of questions, please direct me to it. I'd like to automatically add vectors to a dataframe. I am able to build command strings that would do what I want, but R is not executing them. A simplified example: # Add three vectors called avg_col1, avg_col2, avg_col3 to dataframe df for(colname in c(col1, col2, col3)){ print(paste(df$avg_,colname, - 0;, sep='')) # Just using this to make sure the command is correct paste(avg_,colname, - 0;, sep='') # Does nothing } Output: [1] df$avg_col1 - 0; [1] df$avg_col2 - 0; [1] df$avg_col3 - 0; Thanks for your help! Best - P __ 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. -- Romain Francois Independent R Consultant +33(0) 6 28 91 30 30 http://romainfrancois.blog.free.fr __ 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.
Re: [R] creating and then executing command strings
On Fri, May 15, 2009 at 3:38 PM, Romain Francois romain.franc...@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@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.
Re: [R] creating and then executing command strings
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.s...@imail.org 801.408.8111 -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r- project.org] On Behalf Of Philipp Schmidt Sent: Friday, May 15, 2009 8:35 AM To: Romain Francois Cc: r-help@r-project.org Subject: Re: [R] creating and then executing command strings On Fri, May 15, 2009 at 3:38 PM, Romain Francois romain.franc...@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@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. __ 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.