如何在 R 中使用 strsplit 制作字符串向量?
How to make a vector of character strings using strsplit in R?
我有一段文本已被抓取到 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
如有任何帮助,我们将不胜感激。如果需要更多信息,请告诉我。
strsplit()
的结果是 list()
个字符:
mytext <- "abc \n 18:19 \n abc \n 7-9 \n abc \n"
text <- strsplit(mytext, "\n")
class(text)
[1] "list"
列表中的每个元素都是 class character
summary(text[[1]])
Length Class Mode
5 character character
要将列表转换为向量,您可以 unlist()
它:
text=unlist(text)
summary(text)
Length Class Mode
5 character character
其他选项是 scan
将字符串直接 vector
summary(scan(text=text, what='', quiet=TRUE))
# Length Class Mode
# 5 character character
我有一段文本已被抓取到 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
如有任何帮助,我们将不胜感激。如果需要更多信息,请告诉我。
strsplit()
的结果是 list()
个字符:
mytext <- "abc \n 18:19 \n abc \n 7-9 \n abc \n"
text <- strsplit(mytext, "\n")
class(text)
[1] "list"
列表中的每个元素都是 class character
summary(text[[1]])
Length Class Mode
5 character character
要将列表转换为向量,您可以 unlist()
它:
text=unlist(text)
summary(text)
Length Class Mode
5 character character
其他选项是 scan
将字符串直接 vector
summary(scan(text=text, what='', quiet=TRUE))
# Length Class Mode
# 5 character character