0

I am trying to understand whether I can pass a file with a data dictionary into filter function ?

This is my code:

 dplyr::filter(username != 'kgrf808' & username != 'kkwf800' & username != 'kpzl632')

Yet, instead of passing the ''kgrf808', etc , I want to pass a column of a csv file, containing all these id's so that it filters it automatically. I may end up with 10 codes.

Is this somehow possible? I want to keep tidyverse functionality as much as I can.

Phil
  • 7,287
  • 3
  • 36
  • 66
GaB
  • 1,076
  • 2
  • 16
  • 29
  • 3
    Import the csv. Say it's name is `df`, and the name of the column with the ID's is `ID`, do `dplyr::filter(! username %in% df$ID)` – Ricardo Semião e Castro May 15 '23 at 16:51
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick May 15 '23 at 17:14
  • 1
    See https://stackoverflow.com/q/15358006/3358272, https://stackoverflow.com/q/42637099/3358272 for comparisons of `==` versus `%in%`. – r2evans May 15 '23 at 19:24
  • @RicardoSemiãoeCastro - the suggested code worked , and it is very elegant, intuitive! Please post it as an answer so that I can thick it as the answer. – GaB May 16 '23 at 04:41

2 Answers2

1

To the same effect, you could use an anti-join, live-loading the text file with users to omit:

## create file with rogue users to filter out:
data.frame(rogue_users = c('kgrf808', 'kkwf800', 'kpzl632')) |>
write.table(file = 'rogue_users.csv')
library(dplyr)

all_users <- data.frame(all_users = c('kgrf808', 'kkwf800', 'kpzl632', 'abc1234')) 

all_users |>
    anti_join(read.table('rogue_users.csv'),
              by = c('all_users' = 'rogue_users')
    )

result:

  all_users
1   abc1234
I_O
  • 4,983
  • 2
  • 2
  • 15
1

Import the csv. Say it's name is df, and the name of the column with the ID's is ID, do:

dplyr::filter(! username %in% df$ID)