Remove all rows in a df that contain values from a list in R
I'm working in R and I have a data.frame that looks like this:
v1 v2 v3
1 131 456
2 4 131
3 685 184
4 124 384
5 13184 2
and I have a list of numbers that I want removed:
Bad <- paste(c("13584", "131", "40", "134"))
and I am trying to remove any rows from the data.frame that have any of the "bad" numbers in V2
I've tried:
new.df <- df %>% filter(!str_detect(df$V2, Bad))
Which removes both lines 1 and 5, when I want just line 1 to be removed. I've tried using fixed(Bad) and it gives me the same result.
I've also tried:
new.df <- df[-match(Bad,df$V2)]
Which gives the error "only 0's may be mixed with negative subscripts" and any regex things I've tried to add have just given me errors.
new.df <- df[!df$V2 %in% Bad]
gives me "error: undefined columns selected"
Please help!
• 537 views
•
link
0 answers
No answers yet.
Log in to answer this question.
Hello shelley.w.peterson!
We believe that this post does not fit the main topic of this site.
This does not look like a bioinformatics question. If it is, please explain how.
For this reason we have closed your question. This allows us to keep the site focused on the topics that the community can help with.
If you disagree please tell us why in a reply below, we'll be happy to talk about it.
Cheers!
You should google "data frame undefined columns selected". That will give you the answer you're looking for.