6

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.

Community
  • 1
  • 1
user546497
  • 63
  • 5

2 Answers2

7

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.

Shane
  • 98,550
  • 35
  • 224
  • 217
  • Wow yes, I had forgotten the unlist around it. Thank you for quick reply! – user546497 Dec 17 '10 at 21:02
  • @user - you could also wrap all of that in an `as.numeric()` if you are going to be dealing with numbers all of the time. If you find yourself doing this frequently, putting all of that into a custom function will save you some time & typing. – Chase Dec 17 '10 at 21:12
  • 1
    The x <- read.csv(textConnection(id_string)) method assumes that you have a header, but would have returned a dataframe with x <- read.table(textConnection(id_string), sep=",") – IRTFM Dec 17 '10 at 21:16
  • I think it's better style to use '[[1]]' instead of – hadley Dec 18 '10 at 13:38
  • 1
    @hadley Instead of using `unlist` and turning it into a vector? I think that it must depend on the circumstance/application. – Shane Dec 18 '10 at 13:44
  • I think it makes more sense in this case because it makes the intent of the algorithm (to operate on a single string) more clear. – hadley Dec 28 '10 at 00:48
0

Sticking with strsplit as outlined above is probably best.

I think read.csv wasn't doing what you expected because it was looking for a header. You could try:

s <- "1,2,5,6,10"
read.csv(textConnection(s), header=FALSE)

explicitly telling it not to look for a header. You'll still have to pull your numbers out of the resulting data.frame though. You might be better off going with the lower level function scan, which will give you the vector of numbers directly:

scan(textConnection(s), sep=",", quiet=TRUE)
David F
  • 1,255
  • 9
  • 12