Better output with dplyr — breaking functions and results

前端 未结 1 1751
夕颜
夕颜 2021-01-23 09:20

This is a long-lasting question, but now I really to solve this puzzle. I\'m using dplyr all the time and I think it is great to summarise variables. However, I

相关标签:
1条回答
  • 2021-01-23 09:45

    Instead of transposing (t) and changing the class types, after the summarise step, do a gather to change it to 'long' format and then spread it back after doing some modifications with separate and unite

    library(tidyverse)
    ds %>% 
      group_by(group) %>% 
      summarise_at(vars(iq, income, math),funs(mean, sd)) %>% 
      gather(key, val, iq_mean:math_sd) %>% 
      separate(key, into = c('key1', 'key2')) %>% 
      unite(group, group, key2) %>%
      spread(group, val)
    
    0 讨论(0)
提交回复
热议问题