r-在使用DLPYR查找简单方法时遇到困难-"mean()"命令的行为与预期不符



我正试图使用dplyr来找到我分组在一起的一列值的简单平均值:

我最初的尝试是按如下方式输入代码:

cust_id_flags_3 = customer_sleep %>% group_by(flags) %>% count(flags) %>% summarise(mean_val = mean(n))

但我得到的输出是一个表格

# A tibble: 27 x 2
flags mean_val
<dbl>    <dbl>
1     0     1966
2     1     2555
3     2     1263
4     3     1694
5     4     1452
6     5      989
7     6      879
8     7      709
9     8      712
10     9      530
# ... with 17 more rows

我想要的是mean_val列中值的平均值。我可以通过手动计算得到它:

> mean_test = sum(cust_id_flags_3$mean_val)/nrow(cust_id_flags_3) 
> mean_test
[1] 569.037

下面是我用来进行计算的数据集。但我知道是我在应用tidyverse动词时做错了什么。在上下文中,我这样做是为了能够说明使用泊松回归的方法。感谢您的帮助。

> dput(cust_id_flags_3)
structure(list(flags = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26), 
n = c(1966L, 2555L, 1263L, 1694L, 1452L, 989L, 879L, 709L, 
712L, 530L, 526L, 435L, 398L, 334L, 233L, 174L, 145L, 114L, 
86L, 61L, 36L, 25L, 21L, 13L, 3L, 7L, 4L)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -27L), groups = structure(list(
flags = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26), .rows = structure(list(
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 
14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 
25L, 26L, 27L), ptype = integer(0), class = c("vctrs_list_of", 
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -27L), .drop = TRUE))

您的数据已经分组,我可以通过复制569的平均值

library(dplyr)
df %>% 
ungroup() %>%
summarise(mean_val = mean(n))

现在,每组(标志(只有一个值,因此平均值始终为值/1。如果我调整您的数据,使每个组包含更多的值,则group_by()命令与summarise结合使用将按预期工作。

df <- tibble(
flags = c(
0, 1, 1, 2, 2, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26
),
n = c(
1966L, 2555L, 1263L, 1694L, 1452L, 989L, 879L, 709L,
712L, 530L, 526L, 435L, 398L, 334L, 233L, 174L, 145L, 114L,
86L, 61L, 36L, 25L, 21L, 13L, 3L, 7L, 4L
)
)
df %>% 
group_by(flags) %>%
summarise(mean_val = mean(n), count = n())

count = n()为每组的观察次数添加一个整数。

最新更新