准备发布表格或如何以优雅的方式将均值和标准差结果汇总在一起

时间:2019-09-06 16:53:23

标签: r dplyr knitr kable

我知道dplyr和所有tidyverse软件包对于呈现表非常有用。最近,在处理表时,我开始使用其他包(例如descrqwraps2tangram)。但是,它们都无法达到我想要的结果,我也不想摆脱整洁的环境。

问题是: 假设我有这张桌子:

table

结果来自此数据集:

ds <- structure(list(ID = structure(c(20192, 10201, 20220, 20231, 10018, 
                                      20003, 20217, 20012, 10223, 20227), label = "ID", format.spss = "F5.0"), 
                    group = c("Experimental", "Experimental", "Experimental", 
                    "Experimental", "Experimental", "Attention-control", "Attention-control", 
                    "Attention-control", "Experimental", "Attention-control"), 
                    m_knowledge_before = c(4, 4, 2, 10, 9, 12, 6, 5, 8, 8), m_knowledge_after = c(8, 
                    4, 4, 8, 12, 11, 7, 4, 10, 9), inf_beh_before = c(0.0900144958053339, 
                    -0.143533634873114, -2.89590655323344, 5.15429686825043, 
                    1.24102026593722, -1.06099127432656, 1.25091817975821, -1.06099127432656, 
                    -1.06099127432656, 0.0900144958053339), inf_beh_after = c(-0.78992544399981, 
                    -0.421030298875151, -2.88049939500682, -1.90832990681645, 
                    5.62217186992617, 1.67540316585319, -2.88049939500682, -0.78992544399981, 
                    1.81577862675814, -0.561405759780097), m_beh_before = c(0.15179828765505, 
                    -3.6251065882949, -0.657959208512676, 3.04808420492967, 3.12378180223424, 
                    -0.794845998730921, -1.73665415031992, 0.217823615365689, 
                    -0.794845998730921, -0.723984536223314), m_beh_after = c(1.7782802889654, 
                    -0.0976665054474648, -1.03392504170677, -2.72738723109948, 
                    4.41143073651167, 2.53548394209881, -3.75660293081599, -0.944397600143819, 
                    2.62501138366175, 1.68875284740245)), class = c("tbl_df", 
                    "tbl", "data.frame"), row.names = c(NA, -10L))

现在我想将此代码转换为上面的表格

ds %>% 
  group_by(group) %>% 
  summarise_at(vars(m_knowledge_before, m_knowledge_after,
                    inf_beh_before, inf_beh_after, 
                    m_beh_before, m_beh_after),
                   list(~mean(.),~sd(.)))

也欢迎有线解决方案。 谢谢。

用于重现结果的所有代码:

ds <- structure(list(ID = structure(c(20192, 10201, 20220, 20231, 10018, 
                                      20003, 20217, 20012, 10223, 20227), label = "ID", format.spss = "F5.0"), 
                    group = c("Experimental", "Experimental", "Experimental", 
                    "Experimental", "Experimental", "Attention-control", "Attention-control", 
                    "Attention-control", "Experimental", "Attention-control"), 
                    m_knowledge_before = c(4, 4, 2, 10, 9, 12, 6, 5, 8, 8), m_knowledge_after = c(8, 
                    4, 4, 8, 12, 11, 7, 4, 10, 9), inf_beh_before = c(0.0900144958053339, 
                    -0.143533634873114, -2.89590655323344, 5.15429686825043, 
                    1.24102026593722, -1.06099127432656, 1.25091817975821, -1.06099127432656, 
                    -1.06099127432656, 0.0900144958053339), inf_beh_after = c(-0.78992544399981, 
                    -0.421030298875151, -2.88049939500682, -1.90832990681645, 
                    5.62217186992617, 1.67540316585319, -2.88049939500682, -0.78992544399981, 
                    1.81577862675814, -0.561405759780097), m_beh_before = c(0.15179828765505, 
                    -3.6251065882949, -0.657959208512676, 3.04808420492967, 3.12378180223424, 
                    -0.794845998730921, -1.73665415031992, 0.217823615365689, 
                    -0.794845998730921, -0.723984536223314), m_beh_after = c(1.7782802889654, 
                    -0.0976665054474648, -1.03392504170677, -2.72738723109948, 
                    4.41143073651167, 2.53548394209881, -3.75660293081599, -0.944397600143819, 
                    2.62501138366175, 1.68875284740245)), class = c("tbl_df", 
                    "tbl", "data.frame"), row.names = c(NA, -10L))

ds %>% 
  group_by(group) %>% 
  summarise_at(vars(m_knowledge_before, m_knowledge_after,
                    inf_beh_before, inf_beh_after, 
                    m_beh_before, m_beh_after),
                   list(~mean(.),~sd(.)))

0 个答案:

没有答案