2

I am really new to R, and I am trying to pick it up myself. I got a code online and not really understand why there is ungroup() in the code below. To my understanding, there is no aggregate function, why we need to ungroup??

a<- a%>% 
  rowwise() %>% 
  mutate(b= sum(c, d, na.rm = TRUE)) %>% 
  ungroup() %>% 
  mutate(e= b* x)
zx8754
  • 52,746
  • 12
  • 114
  • 209
Selena
  • 33
  • 3
  • 6
    It removes the effect of `rowwise()`. Details are given in the online documentation. Even with no aggregation, the effects of `rowwise()` persist. Compare the outputs from `print(a)` with and without the call to `rowwise()` in the construction of `a`. Welcome to SO! – Limey Feb 03 '22 at 15:35
  • 3
    As @Limey mentioned, the definition of `rowwise` is **Group** input by rows – akrun Feb 03 '22 at 15:38
  • 5
    Since you're learning R, it might be helpful to make another object (`temp`), where you remove the `rowwise()` grouping, and see how it changes the result: `temp <- a %>% mutate(b= sum(c, d, na.rm = TRUE))` – Matt Feb 03 '22 at 15:42
  • Thank you guys so much!!!!!!!!!! – Selena Feb 03 '22 at 15:47

0 Answers0