1

I have 36 dataframes (divided in two groups H and E; 18 for each). All my dataframes carry a column with multiple words called 'AA'. I would like to remove all the words in H that exists in E and keep only the words unshared between H or E.

Do you have any idea of how I could do it ?

Thank you for your help !

Phil
  • 7,287
  • 3
  • 36
  • 66
Asûra
  • 15
  • 3
  • What do you mean by group? Two lists with 18 data frames each? Or they're not grouped at all, but for you the 36 data frames represent 2 groups? And you mean "remove all rows in H..." right? – mribeirodantas Dec 15 '21 at 23:27
  • Two lists of 18 data frames, yes ! No, I would like to remove all rows in H and E that shared a commun words like "chocolate" in a colmun called 'AA' ('AA' is present in all my dataframes). – Asûra Dec 16 '21 at 08:37

1 Answers1

2

Ok, I tried to simulate your environment. I have a list my_dfs with 4 data frames, H1, H2, E1, E2, belonging to two groups, H and E, as you can imagine. See the R code below.

H1 <- data.frame(Name = c('Marcel', 'Bob', 'John'),
                 AA = c('Soccer', 'Swimming', 'Baseball'))

H2 <- data.frame(Age = c('20', '41', '22'),
                 AA = c('something', 'something4', 'something5'))

E1 <- data.frame(Age = c('20', '41', '22'),
                 AA = c('something', 'something2', 'something3'))
E2 <- data.frame(Age = c('20', '41', '22'),
                 AA = c('Basketball', 'Voleyball', 'Baseball'))

my_dfs <- list(H1, H2, E1, E2)

There are some words in the column AA that are common among the 4 data frames and you would like to remove the rows in each data frame that contain a word in AA that is also in the AA column of the others.

purrr::map(seq_along(my_dfs),
           ~ dplyr::anti_join(my_dfs[[.x]],
                              dplyr::bind_rows(my_dfs[-.x]),
                              by = 'AA'))

The code above should solve your problem.

mribeirodantas
  • 339
  • 1
  • 6
  • The example works well but when I tried it on my dfs, I have this error : Error in UseMethod("anti_join") : no applicable method for 'anti_join' applied to an object of class "character" – Asûra Aug 25 '22 at 14:07