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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With