【发布时间】:2020-07-05 11:53:45
【问题描述】:
我有一个超过 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 582
amy 592
amy 250
tom 33
tom 10
tom 40
基本上,这将从每个唯一用户均值中删除任何 2SD 的异常值。 该代码将获取每个唯一用户的平均值,确定其平均值和标准差,然后删除平均值 > 2SD 的值。
输出:
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))
这是我尝试过的:
first define average and standard deviation
ave = ave(df$duration)
sd = sd(df$duration)
然后为此设置某种参数:
for i in df {
remove all if > 2*sd}
我不确定,想要一些建议。
【问题讨论】:
-
您的公式转换为
df %>% group_by(User) %>% filter(duration < (mean(duration) + 2 * sd(duration))) -
请让我试试这个
-
但它不会给出您显示的预期输出,因为 mean + 2* sd iss 861 for 'amy