Peng Yu
2009-Aug-28 15:04 UTC
[R] How to convert a string passed as an argument to a vector?
Hi, $ cat commandArgs.R args=commandArgs(trailingOnly=TRUE) args[1]+10 I have the above code. But the following command line gives me an error. I am wondering what is the correct way to convert a string to a vector? $ Rscript commandArgs.R 1:3> args=commandArgs(trailingOnly=TRUE) > args[1]+10Error in args[1] + 10 : non-numeric argument to binary operator Execution halted Regards, Peng [[alternative HTML version deleted]]
jim holtman
2009-Aug-28 16:53 UTC
[R] How to convert a string passed as an argument to a vector?
If you have it as a character string, then you have to evaluate it:> x <- '1:3' > x + 10Error in x + 10 : non-numeric argument to binary operator No suitable frames for recover()> eval(parse(text=x))+10[1] 11 12 13>On Fri, Aug 28, 2009 at 11:04 AM, Peng Yu<pengyu.ut at gmail.com> wrote:> Hi, > > $ cat commandArgs.R > args=commandArgs(trailingOnly=TRUE) > args[1]+10 > > I have the above code. But the following command line gives me an error. I > am wondering what is the correct way to convert a string to a vector? > > $ Rscript commandArgs.R 1:3 >> args=commandArgs(trailingOnly=TRUE) >> args[1]+10 > Error in args[1] + 10 : non-numeric argument to binary operator > Execution halted > > > Regards, > Peng > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Maybe Matching Threads
- Command line option to an R script running through Rscript
- Rscript, hashbang, and arguments
- How do I make this faster?
- Why this statement does not print anything in an if-statement that includes 'q()'?
- pasar argumentos de consola a un script de R que contiene source