Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use match:

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading