在我的数据集中
mydat=structure(list(code = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 1L,
1L, 1L, 1L, 1L, 1L), .Label = c("25480МСК", "25481МСК"), class = "factor"),
item = c(13163L, 13163L, 13163L, 13163L, 13163L, 13163L,
13164L, 13164L, 13164L, 13164L, 13164L, 13164L), sales = c(1L,
2L, 15L, 1L, 4L, 3L, 3L, 3L, 15L, 4L, 4L, 4L), action = c(0L,
0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L)), .Names = c("code",
"item", "sales", "action"), class = "data.frame", row.names = c(NA,
-12L))
我有2个小组vars代码和项目。这是两组:
25481МСК 13163
25480МСК 13164
我也有行动专栏。它只能有两个值零(0)或一(1)。 我需要通过action = 0计算销售的中位数,然后用该中位数替换所有的(1)。 必须分别为每个组完成该操作。
I.E。所需的输出
code item sales action output
25481МСК 13163 1 0 1
25481МСК 13163 2 0 2
25481МСК 13163 15 1 2
25481МСК 13163 1 0 1
25481МСК 13163 4 0 4
25481МСК 13163 3 0 3
25480МСК 13164 3 0 3
25480МСК 13164 3 0 3
25480МСК 13164 15 1 4
25480МСК 13164 4 0 4
25480МСК 13164 4 0 4
25480МСК 13164 4 0 4
25481МСК 13163
组中销售活动的零值中位数= 2,而活动1 = 15,因此我们替换了2上的活动1 = 15。
请注意,action = 0的sales列的值也应该在输出列中。 如何执行?
答案 0 :(得分:3)
librar(dplyr)
mydat %>% group_by(code,item) %>%
mutate(output=ifelse(action==0,sales,median(sales[action==0],na.rm = TRUE)))
# A tibble: 12 x 5
# Groups: code, item [2]
code item sales action output
<fct> <int> <int> <int> <int>
1 25481МСК 13163 1 0 1
2 25481МСК 13163 2 0 2
3 25481МСК 13163 15 1 2
4 25481МСК 13163 1 0 1
5 25481МСК 13163 4 0 4
6 25481МСК 13163 3 0 3
7 25480МСК 13164 3 0 3
8 25480МСК 13164 3 0 3
9 25480МСК 13164 15 1 4
10 25480МСК 13164 4 0 4
11 25480МСК 13164 4 0 4
12 25480МСК 13164 4 0 4
答案 1 :(得分:3)
library(data.table)
setDT(mydat)
mydat[,
output := ifelse(action, median(sales[!action]), sales),
by = .(code, item)]
code item sales action output
1: 25481MCK 13163 1 0 1
2: 25481MCK 13163 2 0 2
3: 25481MCK 13163 15 1 2
4: 25481MCK 13163 1 0 1
5: 25481MCK 13163 4 0 4
6: 25481MCK 13163 3 0 3
7: 25480MCK 13164 3 0 3
8: 25480MCK 13164 3 0 3
9: 25480MCK 13164 15 1 4
10: 25480MCK 13164 4 0 4
11: 25480MCK 13164 4 0 4
12: 25480MCK 13164 4 0 4
答案 2 :(得分:1)
为了完整起见,这是使用 update join 的另一种方法:
library(data.table)
# compute medians for each group
med <- setDT(mydat)[action == 0L, median(sales), by = .(code, item)][
# append column to pick only rows with action == 1L in join
, action := 1L]
mydat[
# copy sales to output column, thereby coercing to double to match value of median()
, output := as.numeric(sales)][
# join and update selectively
med, on = .(code, item, action), output := V1]
mydat[]
code item sales action output 1: 25481MCK 13163 1 0 1 2: 25481MCK 13163 2 0 2 3: 25481MCK 13163 15 1 2 4: 25481MCK 13163 1 0 1 5: 25481MCK 13163 4 0 4 6: 25481MCK 13163 3 0 3 7: 25480MCK 13164 3 0 3 8: 25480MCK 13164 3 0 3 9: 25480MCK 13164 15 1 4 10: 25480MCK 13164 4 0 4 11: 25480MCK 13164 4 0 4 12: 25480MCK 13164 4 0 4