我正在尝试删除括号之间的字符/数字。首先,在开头加上括号,即(“(3)”),然后在第二对括号中加上任何内容。有时,第二对括号带有不匹配的括号,使事情变得复杂。一个例子:
library(qdapRegex)
n <- c("(1) Apple (Pe(ar)", "(2) Apple (Or(ang)e)", "(3) Banana (Hot(dog)")
c <- rm_between(n,"(",")", extract = TRUE)
理想情况下:
c
> "Apple" "Apple" "Banana"
答案 0 :(得分:3)
似乎您总是需要第二个单词。如果是这种情况,那么这里有几种(直接)的方法,
#Base R
sapply(strsplit(n, ' '), `[`, 2)
[1] "Apple" "Apple" "Banana"
#The always fun, word() from stringr package
stringr::word(n, 2)
[1] "Apple" "Apple" "Banana"
答案 1 :(得分:0)