'remove the unmatched strings in R

I have a data as below and I would like to remove the values if they're not defined in my.Housing vector. many thanks in advance.

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)

Expected Answer

remove.this
N/A
var1
var2


Solution 1:[1]

Use match:

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

Solution 2:[2]

You can do this:

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Maël
Solution 2 pbraeutigm