sum(count) 错误:参数无效 'type'(闭包)
Error in sum(count) : invalid 'type' (closure) of argument
我有一个订单 table 看起来像这样:
country_code other_info order_status
FR 1523 okay
FR 5151 not_okay
FR 41511 not_okay
IE 5151 okay
两列都是 class 字符,但即使将它们转换为因子,我也遇到了同样的问题。
我只想将 order_status 的比例按 country_code 分组。
在 运行 下面的代码之后我得到了这个错误,我真的不知道它是什么意思:
'Error in sum(count) : invalid 'type' (closure) of argument
library(dplyr)
orders %>%
group_by(country_code) %>%
mutate(countT= sum(count)) %>%
group_by(order_status, add=TRUE) %>%
mutate(per=paste0(round(100*count/countT,2),'%'))
我要的是这个:
country_code order_status per
FR okay 33%
FR not_okay 66%
IE okay 100%
非常感谢您的帮助,我相信这并不复杂。
在 OP 的数据集中,没有 'count' 列。如果我们需要创建频率列,则可以在 'country_code' 分组后使用 mutate
中的 n()
或直接 add_count
library(dplyr)
library(stringr)
order %>%
add_count(country_code) %>% # create a column with name 'n'
group_by(country_code, order_status) %>% # grouped by two columns
mutate(per = str_c(round(100 * n()/n, 2), '%')) # do the rest of computation
# A tibble: 3 x 4
# Groups: country_code, order_status [3]
# country_code order_status n per
# <chr> <chr> <int> <chr>
#1 FR okay 2 50%
#2 FR not_okay 2 50%
#3 IE okay 1 100%
或者如果我们需要单行,使用summarise
order %>%
add_count(country_code) %>% # create a column with name 'n'
group_by(country_code, order_status) %>% # grouped by two columns
summarise(per = str_c(round(100 * n()/first(n), 2), '%'))
如果我们想获得汇总输出,将mutate
更改为summarise
数据
order <- structure(list(country_code = c("FR", "FR", "IE"), order_status = c("okay",
"not_okay", "okay")), class = "data.frame", row.names = c(NA,
-3L))
我有一个订单 table 看起来像这样:
country_code other_info order_status
FR 1523 okay
FR 5151 not_okay
FR 41511 not_okay
IE 5151 okay
两列都是 class 字符,但即使将它们转换为因子,我也遇到了同样的问题。
我只想将 order_status 的比例按 country_code 分组。
在 运行 下面的代码之后我得到了这个错误,我真的不知道它是什么意思:
'Error in sum(count) : invalid 'type' (closure) of argument
library(dplyr)
orders %>%
group_by(country_code) %>%
mutate(countT= sum(count)) %>%
group_by(order_status, add=TRUE) %>%
mutate(per=paste0(round(100*count/countT,2),'%'))
我要的是这个:
country_code order_status per
FR okay 33%
FR not_okay 66%
IE okay 100%
非常感谢您的帮助,我相信这并不复杂。
在 OP 的数据集中,没有 'count' 列。如果我们需要创建频率列,则可以在 'country_code' 分组后使用 mutate
中的 n()
或直接 add_count
library(dplyr)
library(stringr)
order %>%
add_count(country_code) %>% # create a column with name 'n'
group_by(country_code, order_status) %>% # grouped by two columns
mutate(per = str_c(round(100 * n()/n, 2), '%')) # do the rest of computation
# A tibble: 3 x 4
# Groups: country_code, order_status [3]
# country_code order_status n per
# <chr> <chr> <int> <chr>
#1 FR okay 2 50%
#2 FR not_okay 2 50%
#3 IE okay 1 100%
或者如果我们需要单行,使用summarise
order %>%
add_count(country_code) %>% # create a column with name 'n'
group_by(country_code, order_status) %>% # grouped by two columns
summarise(per = str_c(round(100 * n()/first(n), 2), '%'))
如果我们想获得汇总输出,将mutate
更改为summarise
数据
order <- structure(list(country_code = c("FR", "FR", "IE"), order_status = c("okay",
"not_okay", "okay")), class = "data.frame", row.names = c(NA,
-3L))