Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R strsplit problem (easy fix?)

Tags:

r

This should be an easy thing to do. The similar examples I have read on here have been a bit more complex and the techniques are not really working for me.

I have a variable called id_string

> typeof(id_string)
[1] "character"

and

> id_string
[1] "1,2,5,6,10"

What I want to do is split these values out and store them in a new variable. Such that, for example:

x[1] = 1
x[4] = 6
x[5] = 10

I tried to do

x <- strsplit(id_string,",") 

to split it by comma but I just get x = "1 2 5 6 10"

I read through this post on here which is similar and thought that something like

x <- read.csv(textConnection(id_string))

would work but to no avail.

Perhaps I am over thinking this. Please let me know if you have any ideas. Thank you.

like image 853
user546497 Avatar asked Dec 17 '10 20:12

user546497


1 Answers

Not sure what you're doing wrong because it works as advertised.

> x <- unlist(strsplit("1,2,5,6,10", ","))
> x
[1] "1"  "2"  "5"  "6"  "10"
> x[1]
[1] "1"

Keep in mind that strsplit returns a list.

like image 99
Shane Avatar answered Sep 18 '22 16:09

Shane