[R] converting a string to an integer vector

Sarah Goslee sarah.goslee at gmail.com
Thu Oct 18 17:26:55 CEST 2012


Depending on what exactly you are trying to accomplish:

> as.numeric(unlist(strsplit(a, ",")))
[1] 1 2

> read.csv(textConnection(a), header=FALSE)
  V1 V2
1  1  2

Sarah

On Thu, Oct 18, 2012 at 9:08 AM, BenM <bmmoskow at amath.washington.edu> wrote:
> Hi All,
>      Thanks in advance for your help.  I'm trying to convert a string to an
> integer vector.  For instance, I will start with
>
> a <- "1,2"
>
> The result I want to end up with will be the equivalent of
>
> c(1,2)
>
> What's the best way to make the conversion?  I've tried using as.integer(a),
> but R seems to balk at that.
>
> Thanks,
>
> Ben
-- 
Sarah Goslee
http://www.functionaldiversity.org




More information about the R-help mailing list