是否有办法在R中使用dplyr打印分组平均值和sds(即在许多随机对照试验报告中观察到的情况,其中报告了安慰剂和药物组的基线人口统计数据)。目前我有:
group_summary<- df %>%
group_by(group) %>%
summarise_at( vars(age, iq), c(mean, sd), na.rm=TRUE)
t(group_summary)
将sds置于手段之下:
[,1] [,2] [,3]
group "drug1" "drug2" "placebo"
age (x, ...) ... "22.72436" "25.06164" "23.54394"
weight (x, ...) ... "57.97143" NA "64.66667"
age (x, na.rm = FALSE) ... "3.793276" "3.930634" "4.623405"
weight (x, na.rm = FALSE) ... "16.03617" NA "15.44099"
理想情况下,我想要的更像是:
[,1] [,2] [,3]
group "drug1" "drug2" "placebo"
age (x, ...) ... "22.7(3.8)" "25.1(3.9)" "23.5(4.6)"
weight (x, ...) ... "56.0(16.0)" NA "64.7(15.4)"
感谢任何建议,尽管如果可能的话,整理方法将是理想的
答案 0 :(得分:0)
我用这个:
df1 <- df %>%
group_by(grouping.variable) %>%
summarise(#x = variable, y = number of decimal places
mean.sd.x = paste0(format(round(mean(x), y),
nsmall = y),"(",format(round(sd(x), y), nsmall = y) ,")"))
答案 1 :(得分:0)
你必须建立@ Pete的答案。如果你想为a,b,c和d这几个变量做这个,那就做吧..
and
我不太喜欢library(tidyverse)
df%>%
group_by(grouping.variable)%>%
summarise_at(vars(a,b,c,d), funs(paste0(format(round(mean(., na.rm = T), y),
nsmall = y),"(",format(round(sd(., na.rm = T), y), nsmall = y) ,")")))%>%
gather(Variable, Value, -grouping.variable)%>%
spread(grouping.variable, Value)
的工作原理。我想使用t()
包中的gather
和spread
。
我希望这会给你你想要的东西。