Home > Software engineering >  How to delete a row if it contains specific values "VALUE1" and "VALUE2" in colu
How to delete a row if it contains specific values "VALUE1" and "VALUE2" in colu

Time:10-16

I have a large list of data frames and as the heading suggests, would like to find a way to remove all the rows which have both "VALUE1" as well as "VALUE2" in them. For example:

weight | height | Z
---------------------------
62      100      NA
65      89       NA
59      88       randomnumbersVALUE1randomtext
66      92       NA
64      90       NA
64      87       randomnumbersVALUE2randomtext
57      84       randomnumbersVALUE2randomtextrandomnumbersVALUE1randomtext

would change into:

weight | height | Z
---------------------------
62      100      NA
65      89       NA
59      88       randomnumbersVALUE1randomtext
66      92       NA
64      90       NA
64      87       randomnumbersVALUE2randomtext

How could I accomplish this for a list of data frames?

CodePudding user response:

library(tidyverse)

df |>
  filter(is.na(Z) | !(str_detect(Z, 'VALUE1') & str_detect(Z, 'VALUE2')))
  • Related