首页 > 解决方案 > 为什么在这个例子中 summarise_if 不能与 bind_rows 一起工作?

问题描述

我正在尝试在示例数据集中使用bind_rowssummarize_if添加总底行。有与此类问题相关的不同帖子,但不完全是我的问题。此外,一些已发布的问题还有很多其他代码和数据,我最终会花费更多时间来尝试找出代码和示例,而不是如何更普遍地应用答案。

考虑到这一点,我有一个简单的样本数据集。

可重现的例子:

library(tidyverse)
library(readxl)

sample_pivot_data <- structure(list(Group = c("A", "B", "A", "A", "A", "B", "B", "B", 
                                "C", "C", "C"), Season = c("Winter", "Summer", "Winter", "Fall", 
                                                           "Spring", "Winter", "Fall", "Spring", "Winter", "Summer", "Summer"
                                ), Expense = c("Insurance", "Rent", "Utilities", "Misc", "Insurance", 
                                               "Rent", "Utilities", "Insurance", "Rent", "Utilities", "Misc"
                                ), Fixed_Variable = c("Fixed", "Fixed", "Variable", "Variable", 
                                                      "Fixed", "Fixed", "Variable", "Variable", "Fixed", "Variable", 
                                                      "Variable"), Amount = c(300, 200, 400, 300, 800, 400, 200, 300, 
                                                                              450, 230, 120)), row.names = c(NA, -11L), class = c("tbl_df", 
                                                                                                                                  "tbl", "data.frame"))


# A look at the data:

    > sample_pivot_data
# A tibble: 11 x 5
   Group Season Expense   Fixed_Variable Amount
   <chr> <chr>  <chr>     <chr>           <dbl>
 1 A     Winter Insurance Fixed             300
 2 B     Summer Rent      Fixed             200
 3 A     Winter Utilities Variable          400
 4 A     Fall   Misc      Variable          300
 5 A     Spring Insurance Fixed             800
 6 B     Winter Rent      Fixed             400
 7 B     Fall   Utilities Variable          200
 8 B     Spring Insurance Variable          300
 9 C     Winter Rent      Fixed             450
10 C     Summer Utilities Variable          230
11 C     Summer Misc      Variable          120

我发现了一个类似的问题,在这篇文章中解决了这个问题,给了我这个有效的解决方案:

# This works, no syntax issues

my_pivot <- sample_pivot_data %>%
  group_by(Group, Fixed_Variable) %>%
  summarize(category_total = sum(Amount)) %>%
  pivot_wider(names_from = Fixed_Variable, values_from = category_total) %>%
  ungroup() %>%
  mutate(GrandTotal = rowSums(.[-1])) %>%
  bind_rows(summarize_all(.,                                                                 
                          funs(if (is.numeric(.))
                            sum(.)
                            else
                              "Grand_Total"))
            ) %>%
  print()


    # A tibble: 4 x 4
  Group      Fixed Variable GrandTotal
  <chr>      <dbl>    <dbl>      <dbl>
1 A           1100      700       1800
2 B            600      500       1100
3 C            450      350        800
4 Grand_Total 2150     1550       3700

当我尝试做同样的事情,但在下面的代码中使用 summarise_if 时,我收到一个错误:
UseMethod("tbl_vars") 中的错误:没有适用于“函数”类对象的“tbl_vars”方法 我看了这里作为错误的可能解决方案,但我没有遵循在这种情况下的应用方式。

# This does not work

my_pivot2 <- sample_pivot_data %>%
  group_by(Group, Fixed_Variable) %>%
  summarize(category_total = sum(Amount)) %>%
  pivot_wider(names_from = Fixed_Variable, values_from = category_total) %>%
  ungroup() %>%
  mutate(GrandTotal = rowSums(.[-1])) %>%
  bind_rows(summarize_if(is.numeric, sum, na.rm = TRUE)) %>%
  print()

如果有人可以解释为什么上述方法不起作用,我将不胜感激。在相关的说明中,我也尝试bind_rows(summarize_all(., list(~if(is.numeric(.)) sum(.) else "Grand_Total" )))了哪个有效,但 RStudio 一直提示我括号不匹配......也许是一个不同的问题,但我想我会提到而不是发布一个完全独立的问题。

标签: rdplyrpivot-tablebindsummarize

解决方案


.中缺少一个summarize_if()。这工作正常:

my_pivot2 <- sample_pivot_data %>%
  group_by(Group, Fixed_Variable) %>%
  summarize(category_total = sum(Amount)) %>%
  pivot_wider(names_from = Fixed_Variable, values_from = category_total) %>%
  ungroup() %>%
  mutate(GrandTotal = rowSums(.[-1])) %>%
  bind_rows(summarize_if(., is.numeric, sum, na.rm = TRUE)) %>%
  print()

给予:

# A tibble: 4 x 4
  Group Fixed Variable GrandTotal
  <chr> <dbl>    <dbl>      <dbl>
1 A      1100      700       1800
2 B       600      500       1100
3 C       450      350        800
4 NA     2150     1550       3700

推荐阅读