我有下面的d2数据框,我想添加一个列,它是x和group和flag的平均值。但是我在mutate代码中添加了标志列,我不知道如何按组AND标志添加平均值。
d = data.frame(x=c(seq(1,5,1),seq(11,15,1),100,1000),group= c(rep("A",5),rep("B",5),"A","B"))
d
d2 = d%>%
group_by(group) %>%
mutate(
U=quantile(x, 0.75) + 1.5*IQR(x),
L=quantile(x, 0.25) - 1.5*IQR(x),
flag = ifelse(x>U | x<L,1,0),
mu = mean(x)
)
as.data.frame(d2)
我已经为结果添加了“结果”向量
x group U L flag mu result
1 1 A 8.5 -1.5 0 19.16667 3
2 2 A 8.5 -1.5 0 19.16667 3
3 3 A 8.5 -1.5 0 19.16667 3
4 4 A 8.5 -1.5 0 19.16667 3
5 5 A 8.5 -1.5 0 19.16667 3
6 11 B 18.5 8.5 0 177.50000 13
7 12 B 18.5 8.5 0 177.50000 13
8 13 B 18.5 8.5 0 177.50000 13
9 14 B 18.5 8.5 0 177.50000 13
10 15 B 18.5 8.5 0 177.50000 13
11 100 A 8.5 -1.5 1 19.16667 100
12 1000 B 18.5 8.5 1 177.50000 1000
note having group_by(group,flag)
returns
Error: unknown variable to group by : flag
答案 0 :(得分:2)
在初始操作后,只需将group_by(group, flag)
添加到链中,然后mutate()
:
d %>%
group_by(group) %>%
mutate(
U = quantile(x, 0.75) + 1.5 * IQR(x),
L = quantile(x, 0.25) - 1.5 * IQR(x),
flag = ifelse(x > U | x < L, 1, 0),
mu = mean(x)) %>%
group_by(group, flag) %>%
mutate(result = mean(x))
给出了:
#Source: local data frame [12 x 7]
#Groups: group, flag [4]
#
# x group U L flag mu result
# <dbl> <fctr> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 1 A 8.5 -1.5 0 19.16667 3
#2 2 A 8.5 -1.5 0 19.16667 3
#3 3 A 8.5 -1.5 0 19.16667 3
#4 4 A 8.5 -1.5 0 19.16667 3
#5 5 A 8.5 -1.5 0 19.16667 3
#6 11 B 18.5 8.5 0 177.50000 13
#7 12 B 18.5 8.5 0 177.50000 13
#8 13 B 18.5 8.5 0 177.50000 13
#9 14 B 18.5 8.5 0 177.50000 13
#10 15 B 18.5 8.5 0 177.50000 13
#11 100 A 8.5 -1.5 1 19.16667 100
#12 1000 B 18.5 8.5 1 177.50000 1000