1

I know this is a very easy question and I was looking to the answers here and here trying to adapt the code for my current question, but I was not successful. So, several times, we need to present tables with mean and standard deviation (and other information) by multiple groups. This is a really common analysis, but when we decide to use tidyverse to run that, sometimes the code becomes hard to deal with.

Gather

I know this code is very close to what I want

all_fpc %>% 
  group_by(arm, Time) %>% 
  summarise_at(vars(sum_fpc), funs(mean, sd)) %>% 
  gather(key, val, mean:sd) %>% 
  separate(key, into = c('key1', 'key2')) %>% 
  print(n=nrow(.))

But I'm stacked here.

To reproduce this dataset, please try this:

ds <- data.frame(arm=c("Group 1","Group 2","Group 3", "Group 4"), 
                 Time = c("First","Second","Third"),
                 result = rnorm(n = 120,mean=10,sd=2))
ds %>% 
  group_by(arm, Time) %>% 
  summarise_at(vars(result), funs(mean, sd)) %>% 
  gather(key, val, mean:sd) %>% 
  print(n=nrow(.))

Second question: Anyone knows any package (based on tidyverse) that can create a pivot table in an easier way?

Edited: As always, thanks for all suggestions! The answer to this question is below.

Luis
  • 978
  • 7
  • 21
  • Do you need `ds %>% group_by(arm, Time) %>% mutate(rn = row_number()) %>% spread(Time, result)` – akrun Oct 10 '18 at 15:14
  • You're using `separate` instead of `spread`. – iod Oct 10 '18 at 15:14
  • I always find `gather` and `spread` confusing, is this what you're looking for, using `reshape2`: `ds %>% reshape2::dcast(arm+Time ~ key, value="val")` – Mako212 Oct 10 '18 at 15:22
  • But equivalently in tidyverse: `ds %>% spread(key,val)` – Mako212 Oct 10 '18 at 15:24
  • 1
    @akrun Thanks much as always. I was revisiting some old codes you gave me and I'm almost there. Could you please explain me how to reproduce this output without external packages ? library(ezsummary) ` ds %>% select(arm,Time,result) %>% group_by(arm,Time) %>% ezsummary(digits = 1)` – Luis Oct 10 '18 at 15:25
  • When you're just reshaping the data, rather than actually calculating summary statistics, you typically don't need an explicit group like you do for other types of operations, because a wide/long transformation implies groups based on your ID variables. – Mako212 Oct 10 '18 at 15:26
  • @Mako212 Thanks. I did not know that. – Luis Oct 10 '18 at 16:09
  • 1
    For pivoting, try `rpivotTable`. Its ugly but functional and surprisingly versatile. – Nettle Oct 10 '18 at 23:36
  • @Nettle, I'll check this command and I'll come here to tell my results! Thanks for sharing!! =D – Luis Oct 12 '18 at 00:18
  • @Nettle, truly amazing! If someone wants to check the final code using this package: ds – Luis Oct 12 '18 at 03:18

1 Answers1

2

If we want to reproduce ezsummary output, just summarise

ds %>% 
   group_by(arm, Time) %>% 
   summarise(variable = 'result', 
             mean = round(mean(result), 1),
             sd = round(sd(result), 1))
divibisan
  • 8,631
  • 11
  • 31
  • 46
akrun
  • 674,427
  • 24
  • 381
  • 486
  • I always ask myself how do you know so much about tidyverse.... I was trying to reply this question with "gather" "unite" inspired on your code here (https://stackoverflow.com/questions/51372723/better-output-with-dplyr-breaking-functions-and-results) Thanks *much*! – Luis Oct 10 '18 at 15:52
  • @Luis Glad to know that it is helping. I do code everyday, learn from other code, questions, other languages. I think as in any other field, practice is the key. Let's say you devote an hour daily (consistently) for a year in coding, you would be much better in it than your starting point. – akrun Oct 10 '18 at 15:55