dplyr::group_by - Change name of second group

Viewed 30

I'd like to group by two columns, and for the third column I'd like the value to remain same for the first group but changed to 'D5_repeat' for second group.

df <- data.frame(x = c(rep('A',2), rep('B',2)),
                 y = c(rep('Zebra',4), rep('Lion',4)),
                 z = c(rep('D5',4), rep('D10',4)))

df %>%
  group_by(x,y) %>%
  ...

Results

#A tibble: 8 × 3
  x     y     z         
  <chr> <chr> <chr>     
1 A     Zebra D5        
2 A     Zebra D5 
3 B     Zebra D5_repeat        
4 B     Zebra D5_repeat 
5 A     Lion  D10       
6 A     Lion  D10
7 B     Lion  D10_repeat       
8 B     Lion  D10_repeat
2 Answers

You could use match(x, unique(x)) to get the group numbers of x in each y group.

library(dplyr)

df %>%
  group_by(y) %>%
  mutate(z = ifelse(match(x, unique(x)) > 1, paste0(z, '_repeat'), z)) %>%
  ungroup()

# # A tibble: 8 × 3
#   x     y     z
#   <chr> <chr> <chr>     
# 1 A     Zebra D5
# 2 A     Zebra D5
# 3 B     Zebra D5_repeat
# 4 B     Zebra D5_repeat
# 5 A     Lion  D10
# 6 A     Lion  D10
# 7 B     Lion  D10_repeat
# 8 B     Lion  D10_repeat

I think this is what're looking for?

df %>%
  group_by(x,y) %>%
  mutate(z = paste0(z, if_else(row_number() > 1, "_repeat", ""))) %>%
  ungroup()

Result

# A tibble: 8 × 3
  x     y     z         
  <chr> <chr> <chr>     
1 A     Zebra D5        
2 A     Zebra D5_repeat 
3 B     Zebra D5        
4 B     Zebra D5_repeat 
5 A     Lion  D10       
6 A     Lion  D10_repeat
7 B     Lion  D10       
8 B     Lion  D10_repeat
Related