在此示例中,为什么summary_if与bind_rows不兼容?

问题描述 投票:0回答:1

我正在尝试使用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

我发现了类似的问题,该问题在本帖子here中得到了解决,该问题为我提供了有效的解决方案:]

# 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

[当我尝试做同样的事情,但是对下面的代码使用summary_if时,出现错误:UseMethod(“ tbl_vars”)中的错误:没有将适用于'tbl_vars'的适用方法应用于类“ function”的对象]

我将here视为该错误的可能解决方案,但在这种情况下我没有遵循此方法。
# 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一直给我指示括号不匹配的迹象……也许是一个不同的问题,但我认为我会提到而不是发布一个完全独立的问题。

我正在尝试使用bind_rows和summary_if在示例数据集中添加底部总行。有与此问题类型相关的不同帖子,但与我的问题不完全相同。 ...

r dplyr pivot-table bind summarize
1个回答
0
投票

.中缺少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()
© www.soinside.com 2019 - 2024. All rights reserved.