find out in which columns are not the same values by group

I have this database:


structure(list(id = c(2, 2, 5, 5, 5, 7, 7, 8, 8), category = c("A", 
"A", "A", "A", "B", "C", "B", "D", "D"), country = c("France", 
"France", "Spain", "Canada", "Canada", "France", "Spain", "Argentina", 
"Argentina"), Sport = c("Futbol", "Basket", "Basket", "Basket", 
"Basket", "Futbol", "Basket", "Futbol", "Futbol")), row.names = c(NA, 
-9L), class = c("tbl_df", "tbl", "data.frame"))

# A tibble: 9 x 4
     id category country   Sport 
  <dbl> <chr>    <chr>     <chr> 
1     2 A        France    Futbol
2     2 A        France    Basket
3     5 A        Spain     Basket
4     5 A        Canada    Basket
5     5 B        Canada    Basket
6     7 C        France    Futbol
7     7 B        Spain     Basket
8     8 D        Argentina Futbol
9     8 D        Argentina Futbol

I need to group by id and find out in which columns the id's don't have the same values.
In this case, the output should indicate that id 2 does not have the same values ​​in Sport, id 5 in category and country, and id 7 in all columns.
Thanks!

The Unique data frame shows TRUE if there is only one value in the column and FALSE otherwise.

library(dplyr)
DF <- structure(list(id = c(2, 2, 5, 5, 5, 7, 7, 8, 8), 
                 category = c("A", "A", "A", "A", "B", "C", "B", "D", "D"), 
                 country = c("France", "France", "Spain", "Canada", "Canada", "France", "Spain", "Argentina", "Argentina"), 
                 Sport = c("Futbol", "Basket", "Basket", "Basket", "Basket", "Futbol", "Basket", "Futbol", "Futbol")), 
            row.names = c(NA, -9L), class = c("tbl_df", "tbl", "data.frame"))

Unique <- DF |> group_by(id) |> 
  summarize(across(category:Sport, .fns = ~length(unique(.)) == 1))
Unique
#> # A tibble: 4 x 4
#>      id category country Sport
#>   <dbl> <lgl>    <lgl>   <lgl>
#> 1     2 TRUE     TRUE    FALSE
#> 2     5 FALSE    FALSE   TRUE 
#> 3     7 FALSE    FALSE   FALSE
#> 4     8 TRUE     TRUE    TRUE

Created on 2022-04-01 by the reprex package (v2.0.1)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.