如何在R向量中分割逗号分隔的值?

R向量中逗号分隔值的分割可以通过以下方式完成:不列出向量的元素,然后使用strsplit函数进行分割。例如,如果我们有一个向量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"

取消列出(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"

取消列出(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"

取消列出(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"

以上是 如何在R向量中分割逗号分隔的值? 的全部内容, 来源链接: utcz.com/z/337230.html

回到顶部