Home > Mobile >  remove the unmatched strings in R
remove the unmatched strings in R

Time:05-11

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

CodePudding user response:

Use match:

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

CodePudding user response:

You can do this:

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