Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to group rows in an unique row for unique column values?

Tags:

dataframe

r

Suppose the next data frame:

df <- data.frame(a=c('A1', 'A1', 'A1', 'A2', 'A2', 'A3'), 
                 b=c('a', 'b', 'c', 'd', 'e', 'f'), c=rep(1, 6))

I am trying to group based on unique values in a column such that expected data frame could look like this:

# a    b        c   
# A1   [a,b,c]  1
# A2   [d,e]    1
# A3   [f]      1

How could I accomplish this task?

like image 768
AlSub Avatar asked Nov 18 '25 02:11

AlSub


1 Answers

We could group by 'a', 'c', summarise the unique elements to 'b' in a string

library(dplyr)
df %>% 
   group_by(a, c) %>% 
   summarise(b = sprintf('[%s]', toString(unique(b))), .groups = 'drop') %>%
   select(names(df))

-output

# A tibble: 3 x 3
#  a     b             c
#  <chr> <chr>     <dbl>
#1 A1    [a, b, c]     1
#2 A2    [d, e]        1
#3 A3    [f]           1

Or if the 'c' values are also changing, use across

df %>%
  group_by(a) %>%
  summarise(across(everything(), ~ sprintf('[%s]', 
       toString(unique(.)))), .groups = 'drop')

Or if we need a list

df %>%
  group_by(a) %>%
  summarise(across(everything(), ~ list(unique(.))
         
    ), .groups = 'drop')

Or using glue

df %>%
   group_by(a, c) %>%
   summarise(b = glue::glue('[{toString(unique(b))}]'), .groups = 'drop')

-output

# A tibble: 3 x 3
#  a         c b        
#* <chr> <dbl> <glue>   
#1 A1        1 [a, b, c]
#2 A2        1 [d, e]   
#3 A3        1 [f]    
like image 59
akrun Avatar answered Nov 19 '25 17:11

akrun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!