# filter out multiple rows based on the content of one element in a single row

I have a dataframe with many rows that are all repeated values except for the values in column `dd`.

If any one row contains the value "ACT" in this non-repeated column I need to remove all the rows which otherwise match this "ACT" row as well as the "ACT" row itself. So in the sample code I would want to retain only the six rows which contain "c" and "e" in column `aa`.

I have tried a variety of nested if-else with for loops and trying to somehow filter out based on value in `aa` when the "ACT" exist in `dd` but have not been able to figure out how to get away from the single row vector matching.

``````aa <- c("b","b","b","c","c","c","d","d","d","e","e","e")
bb <- c("t","t","t","w","w","w","r","r","r","s","s","s")
cc <- c(1,1,1,2,2,2,3,3,3,4,4,4)
dd <- c("CVR","ACT","CVR","CVR","CVR","CVR","ACT","CVR","CVR","CVR","CVR","CVR")
``````

Ideally I'm look for a `tidyverse` solution but am open to anything of course.

• Using `dplyr` package:
``````library(dplyr)
df1 <- tibble(
aa = c("b","b","b","c","c","c","d","d","d","e","e","e"),
bb = c("t","t","t","w","w","w","r","r","r","s","s","s"),
cc = c(1,1,1,2,2,2,3,3,3,4,4,4),
dd = c("CVR","ACT","CVR","CVR","CVR","CVR","ACT","CVR","CVR","CVR","CVR","CVR")
)

anti_join(df1, df1[df1\$dd=="ACT", ], by=c("aa","bb","cc"))
#> # A tibble: 6 x 4
#>   aa    bb       cc dd
#>   <chr> <chr> <dbl> <chr>
#> 1 c     w         2 CVR
#> 2 c     w         2 CVR
#> 3 c     w         2 CVR
#> 4 e     s         4 CVR
#> 5 e     s         4 CVR
#> 6 e     s         4 CVR
``````
• Using `data.table` package:
``````library(data.table)
df2 <- data.table(
aa = c("b","b","b","c","c","c","d","d","d","e","e","e"),
bb = c("t","t","t","w","w","w","r","r","r","s","s","s"),
cc = c(1,1,1,2,2,2,3,3,3,4,4,4),
dd = c("CVR","ACT","CVR","CVR","CVR","CVR","ACT","CVR","CVR","CVR","CVR","CVR")
)

df2[!df2[dd=="ACT",], on = c("aa","bb","bb")]
#>    aa bb cc  dd
#> 1:  c  w  2 CVR
#> 2:  c  w  2 CVR
#> 3:  c  w  2 CVR
#> 4:  e  s  4 CVR
#> 5:  e  s  4 CVR
#> 6:  e  s  4 CVR
``````

Created on 2019-06-19 by the reprex package (v0.3.0)

You can put the vectors in a data.table and keep only the (aa, bb, cc) groups which don't have "ACT" in the dd column.

``````library(data.table)

df <- data.table(
aa = c("b","b","b","c","c","c","d","d","d","e","e","e"),
bb = c("t","t","t","w","w","w","r","r","r","s","s","s"),
cc = c(1,1,1,2,2,2,3,3,3,4,4,4),
dd = c("CVR","ACT","CVR","CVR","CVR","CVR","ACT","CVR","CVR","CVR","CVR","CVR")
)

df[, if(!"ACT" %in% dd) .SD, .(aa, bb, cc)]
#    aa bb cc  dd
# 1:  c  w  2 CVR
# 2:  c  w  2 CVR
# 3:  c  w  2 CVR
# 4:  e  s  4 CVR
# 5:  e  s  4 CVR
# 6:  e  s  4 CVR
``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.95736384,"math_prob":0.9983457,"size":903,"snap":"2019-35-2019-39","text_gpt3_token_len":284,"char_repetition_ratio":0.14238042,"word_repetition_ratio":0.0,"special_character_ratio":0.3433001,"punctuation_ratio":0.20416667,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98647,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-09-18T15:00:13Z\",\"WARC-Record-ID\":\"<urn:uuid:6b1fafe8-26ac-4489-8ef0-63e9d4dec453>\",\"Content-Length\":\"145420\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:4eaf853c-1cfb-418b-a0fc-bd208cc2b3b0>\",\"WARC-Concurrent-To\":\"<urn:uuid:11e177b8-3a3e-4e6f-8fc4-0892c30a637b>\",\"WARC-IP-Address\":\"151.101.1.69\",\"WARC-Target-URI\":\"https://stackoverflow.com/questions/56672555/filter-out-multiple-rows-based-on-the-content-of-one-element-in-a-single-row/56673165\",\"WARC-Payload-Digest\":\"sha1:ZV2EWKMJPN5VZQEHNXTW2P24POMH25P2\",\"WARC-Block-Digest\":\"sha1:DYE2YCNEA6P75AUE35O7G7VYWP7PKISX\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-39/CC-MAIN-2019-39_segments_1568514573289.83_warc_CC-MAIN-20190918131429-20190918153429-00500.warc.gz\"}"}