在 R 中计算和追加数据框中 Select 列的列总数

Calculating and Appending Column Totals of Select Columns in a Data Frame in R

我有以下代码用于计算某些感兴趣的数量,特别是最右边两列的总和。

library(dplyr)
library(janitor)

m = c(0, 0.8, 2.3, 4.1, 2.1)
l = c(0.3, 0.8, 0.9, 0.75, 0.25)

mytable = data.frame(l, m)
rownames(mytable) = paste("Group", 1:5)

# Initial population
n0 = c(1,1,1,1,1)

mytable = mytable %>%
  mutate(lm = l * m) %>%
  mutate(n = n0) %>%
  mutate(offspring = lm * n) %>%
  adorn_totals("row") 

这给出了以下输出:

> mytable
     l   m    lm n offspring
   0.3 0.0 0.000 1     0.000
   0.8 0.8 0.640 1     0.640
   0.9 2.3 2.070 1     2.070
  0.75 4.1 3.075 1     3.075
  0.25 2.1 0.525 1     0.525
 Total 9.3 6.310 5     6.310

我有以下问题:

一个选项是将所需列以外的列转换为character class,然后再更改。关于行名,tibble 不允许使用行名。我们可能需要先用 rownames_to_column

创建一个列
library(dplyr)
library(tibble)
library(janitor)
out <- mytable %>% 
    rownames_to_column('rn') %>%
    mutate(lm = l *m, n = n0, offspring = lm * n) %>% 
    mutate(across(-c(n, offspring), as.character)) %>% 
    adorn_totals('row', fill = NA) %>%
    type.convert(as.is = TRUE)

-输出

> out
      rn    l   m    lm n offspring
 Group 1 0.30 0.0 0.000 1     0.000
 Group 2 0.80 0.8 0.640 1     0.640
 Group 3 0.90 2.3 2.070 1     2.070
 Group 4 0.75 4.1 3.075 1     3.075
 Group 5 0.25 2.1 0.525 1     0.525
   Total   NA  NA    NA 5     6.310
> str(out)
Classes ‘tabyl’ and 'data.frame':   6 obs. of  6 variables:
 $ rn       : chr  "Group 1" "Group 2" "Group 3" "Group 4" ...
 $ l        : num  0.3 0.8 0.9 0.75 0.25 NA
 $ m        : num  0 0.8 2.3 4.1 2.1 NA
 $ lm       : num  0 0.64 2.07 3.075 0.525 ...
 $ n        : int  1 1 1 1 1 5
 $ offspring: num  0 0.64 2.07 3.075 0.525 ...
 - attr(*, "core")='data.frame':    5 obs. of  6 variables:
  ..$ rn       : chr [1:5] "Group 1" "Group 2" "Group 3" "Group 4" ...
  ..$ l        : chr [1:5] "0.3" "0.8" "0.9" "0.75" ...
  ..$ m        : chr [1:5] "0" "0.8" "2.3" "4.1" ...
  ..$ lm       : chr [1:5] "0" "0.64" "2.07" "3.075" ...
  ..$ n        : num [1:5] 1 1 1 1 1
  ..$ offspring: num [1:5] 0 0.64 2.07 3.075 0.525
 - attr(*, "tabyl_type")= chr "two_way"
 - attr(*, "totals")= chr "row"

关于第一点和第三点:您可以通过为 adorn_totals()... 参数指定列名来控制汇总哪些列。使用 ... 需要为其他参数指定值,即使它们为空,因此下面的 ,,,, 接受这些参数的默认值。

默认情况下会跳过第一列,因为这通常是一个组 ID(就像您的行名),但您可以指定它应该被合计。

下面是 lnoffspring 列的总计:

mytable %>%
  mutate(lm = l * m) %>%
  mutate(n = n0) %>%
  mutate(offspring = lm * n) %>%
  adorn_totals("row",,,,l, n, offspring) 

Returns:

    l   m    lm n offspring
 0.30   0     0 1     0.000
 0.80 0.8  0.64 1     0.640
 0.90 2.3  2.07 1     2.070
 0.75 4.1 3.075 1     3.075
 0.25 2.1 0.525 1     0.525
 3.00   -     - 5     6.310

伴随警告:

Because the first column was specified to be totaled, it does not contain the label 'Total' (or user-specified name) in the totals row