Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comma separated string split

Tags:

r

I'm trying convert this vector string

a<-c("1,2,3","344")

into this

a<-c("1","2","3","344")

I'm using the following code:

a<-c("1,2,3","344")

b<-strsplit(a, ",")

c<-sapply( b, paste0, collapse=",")

But I'm getting back to the original vector:

c<-c("1,2,3","344")

Any help would be appreciated.

Thanks,

Albit

like image 875
albit paoli Avatar asked Dec 04 '25 15:12

albit paoli


2 Answers

Try:

a <- c("1,2,3","344")
scan(text = a, sep = ",", what = "")
# [1] "1"   "2"   "3"   "344"
like image 142
A5C1D2H2I1M1N2O1R2T1 Avatar answered Dec 07 '25 05:12

A5C1D2H2I1M1N2O1R2T1


This solution is identical to the one on my comment:

a <- c("1,2,3", "344")
b <- unlist(strsplit(a, ","))
b
[1] "1"   "2"   "3"   "344" 
like image 21
Enrique Pérez Herrero Avatar answered Dec 07 '25 04:12

Enrique Pérez Herrero



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!