向交叉表添加行和列计数和的Dplyr解决方案

ekqde3dh  于 2023-02-01  发布在  其他
关注(0)|答案(3)|浏览(178)

当第一列包含非数值数据时(因为它充当了行名称),我想准备一个包含行和列之和的交叉表。下面的示例R代码产生了我正在寻找的行计数之和。

mtcars %>%
  as.tibble(.) %>%                                      # make as tibble
  mutate(cyl = factor(cyl), gear = factor(gear)) %>%    # make some factor vars
  select(cyl, gear, mpg) %>%                            # select demo data
  group_by(cyl, gear) %>%                               # two groupings
  tally() %>%                                           # compute tallys
  spread(cyl, n) %>%                                    # make cross table 
  mutate(RowCount = rowSums(.[ ,-1], na.rm = TRUE))     # compute row sums

这就产生了

A tibble: 3 × 5
  gear    `4`   `6`   `8` RowCount
  <fct> <int> <int> <int>    <dbl>
1 3         1     2    12       15
2 4         8     4    NA       12
3 5         2     1     2        5

然而,我正在努力寻找语法来给予我四列468和“RowCount”的总和,并将其作为额外的一行添加到tibble中,作为新的第4行,大概用NA作为“齿轮”总和。注意,这里的最终目的是格式化数据,以便直接输入到一个漂亮的表格中。

uubf1zoe

uubf1zoe1#

下面是使用bind_rows的方法

library(dplyr)
library(tidyr)

mtcars %>% 
  group_by(gear, cyl) %>% 
  summarize(n = n(), .groups = "drop") %>% 
  pivot_wider(names_from = cyl, values_from = n) %>% 
  mutate(RowCount = rowSums(across(`4`:`8`), na.rm = T)) %>% 
  bind_rows(., colSums(.[-1], na.rm = T))
# A tibble: 4 × 5
   gear   `4`   `6`   `8` RowCount
  <dbl> <dbl> <dbl> <dbl>    <dbl>
1     3     1     2    12       15
2     4     8     4    NA       12
3     5     2     1     2        5
4    NA    11     7    14       32
thigvfpy

thigvfpy2#

你可以尝试这样做,添加一个最终的rbind,它只对你想要的列求和:

mtcars %>%
  tibble(.) %>%                                      # make as tibble
  mutate(cyl = factor(cyl), gear = factor(gear)) %>%    # make some factor vars
  select(cyl, gear, mpg) %>%                            # select demo data
  group_by(cyl, gear) %>%                               # two groupings
  tally() %>%                                           # compute tallys
  spread(cyl, n) %>%                                    # make cross table 
  mutate(RowCount = rowSums(.[ ,-1], na.rm = TRUE)) %>%  # compute row sums
  rbind(c(as.factor(NA), colSums(.[ ,-1], na.rm = TRUE)))

输出:

# A tibble: 4 × 5
  gear    `4`   `6`   `8` RowCount
  <fct> <dbl> <dbl> <dbl>    <dbl>
1 3         1     2    12       15
2 4         8     4    NA       12
3 5         2     1     2        5
4 NA       11     7    14       32
6uxekuva

6uxekuva3#

请使用rowSums检查以下代码

mtcars2 <- mtcars
mtcars2$gear <- NA 

bind_rows(mtcars,mtcars2) %>% group_by(gear, cyl) %>% count() %>% 
  pivot_wider(gear, names_from = cyl, values_from = n) %>% 
  mutate(rowcount=rowSums(across(!starts_with('gear')), na.rm=T))

创建于2023年1月28日,使用reprex v2.0.2

# A tibble: 4 × 5
# Groups:   gear [4]
   gear   `4`   `6`   `8` rowcount
  <dbl> <int> <int> <int>    <dbl>
1     3     1     2    12       15
2     4     8     4    NA       12
3     5     2     1     2        5
4    NA    11     7    14       32

相关问题