如何在R中导出标记文本?

时间:2017-07-26 22:19:02

标签: r text

想象一下你有文字变量,如:

x = as.character("If you do not 'answer' this question, 'two' persons will 'die' in the next second")

只返回标记为

的引号的最佳函数是什么
> FUNCTION(x)
> [1] "answer" "two"    "die" 

2 个答案:

答案 0 :(得分:1)

您可以使用stringr包...

library(stringr)
y <- str_match_all(x,"\\'([^\\s]+)\\'")[[1]][,2]

y
[1] "answer" "two"    "die"   

如果您将其应用于向量xstr_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"