[R] How to convert a string passed as an argument to a vector?

jim holtman jholtman at gmail.com
Fri Aug 28 18:53:20 CEST 2009


If you have it as a character string, then you have to evaluate it:

> x <- '1:3'
> x + 10
Error 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?




More information about the R-help mailing list