Lelleo
Lelleo

Reputation: 59

Dplyr group_by and replace to create multiple subgroups of the same variable

library(datasets)
library(dplyr)
mtcars %>% 
  group_by(cyl = replace(cyl, cyl %in% c(6,4), "Group1"))  %>% 
  summarise(meanHP =(hp))

Resulting dataframe

Now, I want to create "Group 2" within "cyl", that would be for when its value is 8, still with the replace command, which would replace all 8s with Group2, but I haven't figured out how to do that.

Upvotes: 1

Views: 89

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 389325

You can use fct_collapse from forcats to collapse multiple values in one or more group.

library(dplyr)
library(forcats)

mtcars %>%
  group_by(cyl = fct_collapse(as.character(cyl), Group1 = c('4', '6'), Group2 = '8')) %>%
  summarise(meanHP = mean(hp))

#  cyl    meanHP
#  <fct>   <dbl>
#1 Group1   98.1
#2 Group2  209. 

Upvotes: 1

TarJae
TarJae

Reputation: 79311

With replace:

library(dplyr)
mtcars %>% 
  group_by(cyl = replace(cyl, cyl %in% c(6,4), "Group1"))  %>% 
  summarise(meanHP =(hp)) %>% 
  mutate(cyl = replace(cyl, cyl == "8", "Group2")) %>% 
  print(n=40)

We could also use an ifelse statement:

library(dplyr)
mtcars %>% 
  group_by(cyl = replace(cyl, cyl %in% c(6,4), "Group1"))  %>% 
  summarise(meanHP =(hp)) %>% 
  mutate(cyl = ifelse(cyl=="8", "Group2", cyl)) %>% 
  print(n=40)

Output:

# Groups:   cyl [2]
   cyl    meanHP
   <chr>   <dbl>
 1 Group2    175
 2 Group2    245
 3 Group2    180
 4 Group2    180
 5 Group2    180
 6 Group2    205
 7 Group2    215
 8 Group2    230
 9 Group2    150
10 Group2    150
11 Group2    245
12 Group2    175
13 Group2    264
14 Group2    335
15 Group1    110
16 Group1    110
17 Group1     93
18 Group1    110
19 Group1    105
20 Group1     62
21 Group1     95
22 Group1    123
23 Group1    123
24 Group1     66
25 Group1     52
26 Group1     65
27 Group1     97
28 Group1     66
29 Group1     91
30 Group1    113
31 Group1    175
32 Group1    109

Upvotes: 2

Related Questions