我有一个文本块,我已经将其写入R并被读作一个长字符串。
文本块示例:
[1] "abc \n 18:19 \n abc \n 7-9 \n abc \n"
文本块摘要:
summary(text)
Length Class Mode
1 character character
然后我做了一个strsplit text <- strsplit(text, "\n")
strsplit
summary(text)
Length Class Mode
[1,] 5 -none- character
完成strsplit
summary(text)
Length Class Mode
5 character character
任何帮助将不胜感激。如果需要更多信息,请告诉我。
答案 0 :(得分:6)
strsplit()
的结果是list()
字符:
mytext <- "abc \n 18:19 \n abc \n 7-9 \n abc \n"
text <- strsplit(mytext, "\n")
class(text)
[1] "list"
列表的每个元素都是类character
summary(text[[1]])
Length Class Mode
5 character character
要将列表转换为矢量,您可以unlist()
:
text=unlist(text)
summary(text)
Length Class Mode
5 character character
答案 1 :(得分:2)
其他选项是将scan
字符串直接转换为vector
summary(scan(text=text, what='', quiet=TRUE))
# Length Class Mode
# 5 character character