在这个例子中,为什么 summarize_if 不能与 bind_rows 一起使用?
Why won't summarize_if work with bind_rows in this example?
我正在尝试使用 bind_rows
和 summarize_if
在示例数据集中添加总计底行。有不同的 postings 与此类问题相关,但不完全是我的问题。此外,一些 posted 问题有太多其他代码和数据,以至于我花了更多时间试图找出代码和示例,而不是如何更普遍地应用答案。
考虑到这一点,我有一个简单的示例数据集。
可重现的例子:
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
我发现了一个类似的问题,在这个 post 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
当我尝试做同样的事情,但使用 summarize_if 和下面的代码时,我得到一个错误:
使用方法错误("tbl_vars"):
没有适用于 'tbl_vars' 的方法应用于 class "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 一直给我一个指示,即括号不匹配......也许是一个不同的问题,但我想我会提到而不是 post 完全单独的问题。
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
我正在尝试使用 bind_rows
和 summarize_if
在示例数据集中添加总计底行。有不同的 postings 与此类问题相关,但不完全是我的问题。此外,一些 posted 问题有太多其他代码和数据,以至于我花了更多时间试图找出代码和示例,而不是如何更普遍地应用答案。
考虑到这一点,我有一个简单的示例数据集。
可重现的例子:
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
我发现了一个类似的问题,在这个 post 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
当我尝试做同样的事情,但使用 summarize_if 和下面的代码时,我得到一个错误:
使用方法错误("tbl_vars"):
没有适用于 'tbl_vars' 的方法应用于 class "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 一直给我一个指示,即括号不匹配......也许是一个不同的问题,但我想我会提到而不是 post 完全单独的问题。
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