How to split comma separated values in an R vector?

R ProgrammingServer Side ProgrammingProgramming

The splitting of comma separated values in an R vector can be done by unlisting the elements of the vector then using strsplit function for splitting. For example, if we have a vector say x that contains comma separated values then the splitting of those values will be done by using the command unlist(strsplit(x,",")).

Example

 Live Demo

x1<-sample(c("a,b,c,d,e,f,g,h"),30,replace=TRUE)
x1

Output

[1] "a,b,c,d,e,f,g,h" "a,b,c,d,e,f,g,h" "a,b,c,d,e,f,g,h" "a,b,c,d,e,f,g,h"
[5] "a,b,c,d,e,f,g,h" "a,b,c,d,e,f,g,h" "a,b,c,d,e,f,g,h" "a,b,c,d,e,f,g,h"
[9] "a,b,c,d,e,f,g,h" "a,b,c,d,e,f,g,h" "a,b,c,d,e,f,g,h" "a,b,c,d,e,f,g,h"
[13] "a,b,c,d,e,f,g,h" "a,b,c,d,e,f,g,h" "a,b,c,d,e,f,g,h" "a,b,c,d,e,f,g,h"
[17] "a,b,c,d,e,f,g,h" "a,b,c,d,e,f,g,h" "a,b,c,d,e,f,g,h" "a,b,c,d,e,f,g,h"
[21] "a,b,c,d,e,f,g,h" "a,b,c,d,e,f,g,h" "a,b,c,d,e,f,g,h" "a,b,c,d,e,f,g,h"
[25] "a,b,c,d,e,f,g,h" "a,b,c,d,e,f,g,h" "a,b,c,d,e,f,g,h" "a,b,c,d,e,f,g,h"
[29] "a,b,c,d,e,f,g,h" "a,b,c,d,e,f,g,h"

unlist(strsplit(x1,","))

[1] "a" "b" "c" "d" "e" "f" "g" "h" "a" "b" "c" "d" "e" "f" "g" "h" "a" "b"
[19] "c" "d" "e" "f" "g" "h" "a" "b" "c" "d" "e" "f" "g" "h" "a" "b" "c" "d"
[37] "e" "f" "g" "h" "a" "b" "c" "d" "e" "f" "g" "h" "a" "b" "c" "d" "e" "f"
[55] "g" "h" "a" "b" "c" "d" "e" "f" "g" "h" "a" "b" "c" "d" "e" "f" "g" "h"
[73] "a" "b" "c" "d" "e" "f" "g" "h" "a" "b" "c" "d" "e" "f" "g" "h" "a" "b"
[91] "c" "d" "e" "f" "g" "h" "a" "b" "c" "d" "e" "f" "g" "h" "a" "b" "c" "d"
[109] "e" "f" "g" "h" "a" "b" "c" "d" "e" "f" "g" "h" "a" "b" "c" "d" "e" "f"
[127] "g" "h" "a" "b" "c" "d" "e" "f" "g" "h" "a" "b" "c" "d" "e" "f" "g" "h"
[145] "a" "b" "c" "d" "e" "f" "g" "h" "a" "b" "c" "d" "e" "f" "g" "h" "a" "b"
[163] "c" "d" "e" "f" "g" "h" "a" "b" "c" "d" "e" "f" "g" "h" "a" "b" "c" "d"
[181] "e" "f" "g" "h" "a" "b" "c" "d" "e" "f" "g" "h" "a" "b" "c" "d" "e" "f"
[199] "g" "h" "a" "b" "c" "d" "e" "f" "g" "h" "a" "b" "c" "d" "e" "f" "g" "h"
[217] "a" "b" "c" "d" "e" "f" "g" "h" "a" "b" "c" "d" "e" "f" "g" "h" "a" "b"
[235] "c" "d" "e" "f" "g" "h"

Example

 Live Demo

x2<-sample(c("india,russia,canada,china,united kingdom,egypt"),20,replace=TRUE)
x2

Output

[1] "india,russia,canada,china,united kingdom,egypt"
[2] "india,russia,canada,china,united kingdom,egypt"
[3] "india,russia,canada,china,united kingdom,egypt"
[4] "india,russia,canada,china,united kingdom,egypt"
[5] "india,russia,canada,china,united kingdom,egypt"
[6] "india,russia,canada,china,united kingdom,egypt"
[7] "india,russia,canada,china,united kingdom,egypt"
[8] "india,russia,canada,china,united kingdom,egypt"
[9] "india,russia,canada,china,united kingdom,egypt"
[10] "india,russia,canada,china,united kingdom,egypt"
[11] "india,russia,canada,china,united kingdom,egypt"
[12] "india,russia,canada,china,united kingdom,egypt"
[13] "india,russia,canada,china,united kingdom,egypt"
[14] "india,russia,canada,china,united kingdom,egypt"
[15] "india,russia,canada,china,united kingdom,egypt"
[16] "india,russia,canada,china,united kingdom,egypt"
[17] "india,russia,canada,china,united kingdom,egypt"
[18] "india,russia,canada,china,united kingdom,egypt"
[19] "india,russia,canada,china,united kingdom,egypt"
[20] "india,russia,canada,china,united kingdom,egypt"

unlist(strsplit(x2,","))

[1] "india" "russia" "canada" "china"
[5] "united kingdom" "egypt" "india" "russia"
[9] "canada" "china" "united kingdom" "egypt"
[13] "india" "russia" "canada" "china"
[17] "united kingdom" "egypt" "india" "russia"
[21] "canada" "china" "united kingdom" "egypt"
[25] "india" "russia" "canada" "china"
[29] "united kingdom" "egypt" "india" "russia"
[33] "canada" "china" "united kingdom" "egypt"
[37] "india" "russia" "canada" "china"
[41] "united kingdom" "egypt" "india" "russia"
[45] "canada" "china" "united kingdom" "egypt"
[49] "india" "russia" "canada" "china"
[53] "united kingdom" "egypt" "india" "russia"
[57] "canada" "china" "united kingdom" "egypt"
[61] "india" "russia" "canada" "china"
[65] "united kingdom" "egypt" "india" "russia"
[69] "canada" "china" "united kingdom" "egypt"
[73] "india" "russia" "canada" "china"
[77] "united kingdom" "egypt" "india" "russia"
[81] "canada" "china" "united kingdom" "egypt"
[85] "india" "russia" "canada" "china"
[89] "united kingdom" "egypt" "india" "russia"
[93] "canada" "china" "united kingdom" "egypt"
[97] "india" "russia" "canada" "china"
[101] "united kingdom" "egypt" "india" "russia"
[105] "canada" "china" "united kingdom" "egypt"
[109] "india" "russia" "canada" "china"
[113] "united kingdom" "egypt" "india" "russia"
[117] "canada" "china" "united kingdom" "egypt"

Example

 Live Demo

x3<-sample(c("male,female"),20,replace=TRUE)
x3

Output

[1] "male,female" "male,female" "male,female" "male,female" "male,female"
[6] "male,female" "male,female" "male,female" "male,female" "male,female"
[11] "male,female" "male,female" "male,female" "male,female" "male,female"
[16] "male,female" "male,female" "male,female" "male,female" "male,female"

unlist(strsplit(x3,","))

[1] "male" "female" "male" "female" "male" "female" "male" "female"
[9] "male" "female" "male" "female" "male" "female" "male" "female"
[17] "male" "female" "male" "female" "male" "female" "male" "female"
[25] "male" "female" "male" "female" "male" "female" "male" "female"
[33] "male" "female" "male" "female" "male" "female" "male" "female"
raja
Published on 06-Feb-2021 12:45:16
Advertisements