【发布时间】:2020-03-25 00:51:41
【问题描述】:
我有一个超过 10,000 行的大型数据集:df:
User duration
amy 582
amy 27
amy 592
amy 16
amy 250
tom 33
tom 10
tom 40
tom 100
期望的输出:
User duration
amy 293.4
amy 27
amy 293.4
amy 16
amy 250
tom 33
tom 10
tom 40
tom 45.75
我们在这里看到,任何大于 1SD 的不同用户组平均值的值都被删除,然后替换为(唯一用户名的)平均值。 amy 组的平均值为 293.4 组 tom 的平均值为:45.75
输出:
structure(list(User = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L), .Label = c("amy", "tom"), class = "factor"), duration = c(582L,
27L, 592L, 16L, 250L, 33L, 10L, 40L, 100L)), class = "data.frame", row.names = c(NA,
-9L))
这是我按照这里的一位成员的建议尝试过的,效果很好,我不确定现在如何用每个组的平均值替换删除的值:
df %>%
group_by(User) %>%
filter(between(duration, mean(duration) - 1 * sd(duration),
mean(duration) + 1 * sd(duration)))
欢迎提出任何建议
【问题讨论】: