【问题标题】:replace median for category separated by groups in R替换R中由组分隔的类别的中位数
【发布时间】:2018-08-14 09:19:05
【问题描述】:

在我的数据集中

 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 个组变量代码+项目。这里有两组:

25481МСК    13163
25480МСК    13164

我还有行动专栏。它只能有两个值零(0)或一(1)。 我需要通过 action=0 计算销售额的中位数,然后用这个中位数替换 allones(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 group = 2 中销售操作的中位数为零,操作 1=15,因此我们将操作 1=15 替换为 2。

请注意,action=0 的销售列的值也应该在输出列中。 如何执行?

【问题讨论】:

  • 您能否更清楚地了解这个问题。无法理解您的输出
  • 第 2 组的中位数为 4。
  • @Hunaidkhan,我提供了错误的所需输出。请检查编辑
  • 在输出中,我们通过 action =0 m 保留了所有的销售价值,但销售的 action=1 必须用中位数替换。

标签: r dplyr data.table plyr


【解决方案1】:
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

【讨论】:

  • 是的,它得到了想要的输出
【解决方案2】:
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

【讨论】:

  • replace 是另一种选择:mydat[, v := replace(sales, action == 1, median(sales[action == 0])), by=.(code, item)]
  • 谢谢@Frank。我不熟悉replace,但在这种情况下听起来更有效。
【解决方案3】:

为了完整起见,这里是另一种使用更新连接的方法:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    • 1970-01-01
    • 2019-02-04
    相关资源
    最近更新 更多