删除 R 中不匹配的字符串

remove the unmatched strings in R

我有一个如下所示的 data,如果这些值未在 my.Housing 向量中定义,我想删除它们。非常感谢。

remove.this <- c("I do here some text that I want to remove from my dataset",
                 "var1", "var2")

my.Housing <- c("var1", "var2", "var3")

data <- data.frame(remove.this, my.Housing)

setdiff(remove.this,my.Housing)

预期答案

remove.this
N/A
var1
var2

你可以这样做:

my.Housing[remove.this %in% my.Housing]

使用match:

data$my.Housing[match(data$remove.this, data$my.Housing)]
#[1] NA     "var1" "var2"