2017-02-28 152 views
0

我覺得冠軍是有點混亂刪除向量元素,但在這裏我的問題: 我有2個載體,一個包含一些文本含有一些短語R:從矢量元素

text <- c("this is some text","some elements should be removed", "i hope you can help me with this text element problem") 
pattern <- c("text", "some","be") 

而現在另一個我想刪除從pattern這是在文本的所有元素,從而導致矢量

text_result 
[1] "this is" 
[2] "elements should removed" 
[3] "i hope you can help me with this element problem" 

我試圖

text_result <- sapply(pattern, function(x) gsub(x, text, replacement ="")) 

text_result <- sapply(text, function(y) sapply(pattern, function(x)gsub(x,y,replacement =""))) 

但在這兩種情況下,我收到了大量矩陣

length(pattern)*length(text) elements 

在此先感謝!

+0

'GSUB(paste0(模式,塌陷= 「|」), 「」,文)' –

+0

工作得很好,謝謝! –

回答

0

你可以試試:

`%notin%` <- function(x,y) !(x %in% y) 
lapply(strsplit(text," "),function(x) paste(x[x %notin% pattern],collapse=" ")) 
+0

也工作正常,返回列表,謝謝! –