如何在 R 向量中分割逗号分隔值?
可以通过取消列出向量的元素,然后使用 strsplit 函数进行分割来分割 R 向量中的逗号分隔值。例如,如果我们有向量 x ,其中包含逗号分隔的值,那么可以使用命令 unlist(strsplit(x,","))来分割这些值。
示例
x1<-sample(c("a,b,c,d,e,f,g,h"),30,replace=TRUE) x1
输出
[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"
示例
x2<-sample(c("india,russia,canada,china,united kingdom,egypt"),20,replace=TRUE) x2
输出
[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"
示例
x3<-sample(c("male,female"),20,replace=TRUE) x3
输出
[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"
广告