我有一个字符串,其中的条目由;
分隔。
abc;def;tyu;poi;asf;ghl
R中是否有一个函数允许我将此字符串拆分为
abc
def
tyu
所有都是单独的对象,以便我可以单独访问和索引它们吗?
有没有办法在没有角色参数的情况下做到这一点? Ny直接索引数据帧中的单元格?单元格看起来像上面显示的字符串,但如果我尝试
strsplit(k[1,8],split=';')
非字符参数错误。
答案 0 :(得分:4)
items <- scan(text="abc;def;tyu;poi;asf;ghl", sep=";", what="")
Read 6 items
as.matrix(items)
#----------
[,1]
[1,] "abc"
[2,] "def"
[3,] "tyu"
[4,] "poi"
[5,] "asf"
[6,] "ghl"
如果这些是data.frame中的项目,那么 可能使用了read.delim和sep =“;”可能stringsAsFactors = FALSE。您仍然可以使用as.character
df <- data.frame(a="abc;def;tyu;poi;asf;ghl")
items <- scan(text=df[1,1], sep=";", what="")
Error in textConnection(text) : invalid 'text' argument
# Use as.character instead
items <- scan(text=as.character(df[1,1]), sep=";", what="")
Read 6 items
答案 1 :(得分:2)
为此,您可以使用strsplit
功能。
> strsplit('abc;def;ghi', split = ';')
[[1]]
[1] "abc" "def" "ghi"
请注意,strsplit
是矢量化的,它会返回结果列表,如果您只提供一个字符串。只获得一个字符串:
strsplit('abc;def;ghi', split = ';')[[1]]
[1] "abc" "def" "ghi"
向量化的优点是您可以向strsplit
提供字符串向量:
> strsplit(rep('abc;def;ghi', 10), split = ';')
[[1]]
[1] "abc" "def" "ghi"
[[2]]
[1] "abc" "def" "ghi"
[[3]]
[1] "abc" "def" "ghi"
[[4]]
[1] "abc" "def" "ghi"
[[5]]
[1] "abc" "def" "ghi"
[[6]]
[1] "abc" "def" "ghi"
[[7]]
[1] "abc" "def" "ghi"
[[8]]
[1] "abc" "def" "ghi"
[[9]]
[1] "abc" "def" "ghi"
[[10]]
[1] "abc" "def" "ghi"