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.
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.