我是新来的.我需要生成一个Tibble,其中每个变量按一个因子分组,并由"±"分隔的平均值和标准差来描述.

让我们使用虹膜数据集.

iris %>%
  group_by(Species) %>%
  summarise(across(everything(), list(Mean=mean,dev.st=sd))) %>% 
  pivot_longer(cols = -Species, names_to = c(".value", "variable"), names_sep = "_")

我怎么才能继续呢? 先谢谢你

推荐答案

您可以使用更新的dplyr::reframe(取代dplyr::summarize),并将此组合汇总统计信息(comb)添加到您的函数列表中:

library(dplyr)
library(tidyr)

iris %>%
  group_by(Species) %>%
  reframe(across(everything(), 
                 list(Mean = ~ as.character(mean(.x)), 
                      dev.sd = ~ as.character(sd(.x)), 
                      comb = ~ paste(mean(.x), sd(.x), sep = " ± ")))) %>%
  pivot_longer(cols = -Species, names_to = c(".value", "variable"), 
               names_sep = "_")


# (from comment) if you only wanted the combined column and want 
# them at two significant digits, you could adjust:

iris %>%
  group_by(Species) %>%
  reframe(across(everything(), 
                 list(comb = ~ paste(sprintf("%.2f", mean(.x)), 
                                     sprintf("%.2f", sd(.x)), sep = " ± ")))) %>%
  pivot_longer(cols = -Species, names_to = c(".value", "variable"), 
               names_sep = "_")

#' In this case you get the exact same thing if you replace `reframe` with 
#' `summarize`, but the latter is being replaced by `reframe` 
#' by `dplyr` moving forward

注要与pivot_longer组合,所有元素都需要在同一个类中,因此将它们转换为字符.如果保持较宽的范围,则不必在汇总统计数据中添加as.character()位.

输出

  Species    variable Sepal.Length              Sepal.Width               Petal.Length              Petal.Width              
  <fct>      <chr>    <chr>                     <chr>                     <chr>                     <chr>                    
1 setosa     Mean     5.006                     3.428                     1.462                     0.246                    
2 setosa     dev.sd   0.352489687213451         0.379064369096289         0.173663996480184         0.105385589380046        
3 setosa     comb     5.006 ± 0.352489687213451 3.428 ± 0.379064369096289 1.462 ± 0.173663996480184 0.246 ± 0.105385589380046
4 versicolor Mean     5.936                     2.77                      4.26                      1.326                    
5 versicolor dev.sd   0.516171147063863         0.313798323378411         0.469910977239958         0.197752680004544        
6 versicolor comb     5.936 ± 0.516171147063863 2.77 ± 0.313798323378411  4.26 ± 0.469910977239958  1.326 ± 0.197752680004544
7 virginica  Mean     6.588                     2.974                     5.552                     2.026                    
8 virginica  dev.sd   0.635879593274432         0.322496638172637         0.551894695663983         0.274650055636667        
9 virginica  comb     6.588 ± 0.635879593274432 2.974 ± 0.322496638172637 5.552 ± 0.551894695663983 2.026 ± 0.274650055636667

R相关问答推荐

如何识别组内的行是否在同一列中具有值?

分组时间连续值

为什么stat_bin在R中的ggplot中显示错误的数字?

如何在热图中绘制一个图形,但在每个单元格中通过饼形图显示?

如何创建构成多个独立列条目列表的收件箱框列?

在ggplot Likert条中添加水平线

R创建一个数据透视表,计算多个组的百分比

根据模式将一列拆分为多列,并在R中进行拆分

如何在编辑列时更新可编辑数据表,并使用该表在Shiny中执行连续计算

如何改变时间图R中的悬停信息?

在连续尺度上转置标签[瀑布图,R]

解析R函数中的变量时出现的问题

有没有办法使用ggText,<;Sub>;&;<;sup>;将上标和下标添加到同一元素?

您是否可以将组添加到堆叠的柱状图

在R中使用列表(作为tibble列)进行向量化?

Conditional documentr::R中数据帧的summarize()

自定义交互作用图的标签

当由base::限定时,`[.factor`引发NextMethod错误

如何移动点以使它们的打印不重叠

计算多变量的加权和