If the rows of two columns are equal remove and mantaim the others - R

Viewed 39

I have a df with multiple columns

polo      cod ....   nome    students
macaé     656 ....   UFRJ    5000
caxias    789 ....   UFRJ    5000
fundao    234 ....   UFRJ    5000
niteroi   477 ....   UFF     4000
botafogo  323 ....   Unirio  1000 

My desired df will keep these columns but remove the rows that the column name and students are equal

polo                    cod ....   nome    students
macaé_caxias_fundao     656 ....   UFRJ    5000
niteroi                 477 ....   UFF     4000
botafogo                323 ....   Unirio  1000 

The code: DATA = MYDATA %>% distinct(name,students) remove the others columns that are important

Thank you very much!

1 Answers

We may do a group by paste here

library(dplyr)
library(stringr)
MYDATA %>%
 group_by(nome, students) %>%
 summarise(polo = str_c(polo, collapse = "_"), cod = first(cod))
Related