From long to wide data with multiple columns

后端 未结 3 1364
渐次进展
渐次进展 2020-12-06 19:19

Suggestions for how to smoothly get from foo to foo2 (preferably with tidyr or reshape2 packages)?

This is kind of like this question, but not exactly I think, becau

3条回答
  •  生来不讨喜
    2020-12-06 19:46

    Here's another alternative using tidyr:

    library(tidyr)
    foo %>%
      gather(key, value, -group, -times, -num_users) %>%
      unite(col, key, times) %>%
      spread(col, value)
    

    Which gives:

    #  group num_users action_rate_after action_rate_before action_rate_c95_after
    #1     a       100              0.15                0.1            0.06962893
    #2     b       200              0.18                0.2            0.05297400
    #3     c       300              0.35                0.3            0.05369881
    #  action_rate_c95_before
    #1             0.05850000
    #2             0.05515433
    #3             0.05159215
    

提交回复
热议问题