想象一下你有文字变量,如:
x = as.character("If you do not 'answer' this question, 'two' persons will 'die' in the next second")
只返回标记为
的引号的最佳函数是什么> FUNCTION(x)
> [1] "answer" "two" "die"
答案 0 :(得分:1)
您可以使用stringr
包...
library(stringr)
y <- str_match_all(x,"\\'([^\\s]+)\\'")[[1]][,2]
y
[1] "answer" "two" "die"
如果您将其应用于向量x
,str_match_all
将为x
的每个元素生成一个包含2列矩阵的列表。你需要每个的第二列。
答案 1 :(得分:0)
这是使用基本软件包的另一种方式:
x = "If you do not 'answer' this question, 'two' persons will 'die' in the next second"
gsub( "'",
"",
grep(
pattern = "'[a-z]+'",
x = strsplit(x, " ")[[1]],
value = T
)
)
# [1] "answer" "two" "die"