Here is an example of what my data looks like:
group valueA1 valueA2 valueB1 valueB2
1 2 3 4 5
1 4 4 5 5
2 5 4 3 2
I want to create a table with summary statistics organised in a few different ways. Firstly, I need my results organised by group, then by whether it contains A or B, then by whether it contains 1 or 2 in the variable name.
I understand that I can use something like this;
Agroup <- mydata %>%
select(contains("valueA"))%>%
summarise_all(list(mean=mean, sd=sd), na.rm = TRUE)
This gives me the list of means and sd, however. How can I print these as a table?
mydata = data.frame(
group = c(1, 1, 2),
valueA1 = c(2, 4, 5),
valueA2 = c(3, 4, 4),
valueB1 = c(4, 5, 3),
valueB2 = c(5, 5, 2)
)
Agroup <- mydata %>%
select(contains("valueA")) %>%
summarise_all(list(mean=mean, sd=sd), na.rm = TRUE)
Agroup
gives you:
valueA1_mean valueA2_mean valueA1_sd valueA2_sd
1 3.666667 3.666667 1.527525 0.5773503
and
Agroup <- mydata %>%
select(contains("1")) %>%
summarise_all(list(mean=mean, sd=sd), na.rm = TRUE)
Agroup
gives you:
valueA1_mean valueB1_mean valueA1_sd valueB1_sd
1 3.666667 4 1.527525 1
Same applies to contains("valueB")
and "2"
.