【问题标题】:How to subtract a median using complex condition in R如何使用R中的复杂条件减去中位数
【发布时间】:2018-11-21 17:52:14
【问题描述】:

我有数据集

df=structure(list(SKU = c(11202L, 11202L, 11202L, 11202L, 11202L, 
11202L, 11202L, 11202L, 11202L, 11202L, 11202L, 11202L, 11202L, 
11202L, 11202L, 11202L, 11202L, 11202L, 11202L, 11202L, 11202L
), stuff = c(8.85947691, 9.450108704, 10.0407405, 10.0407405, 
10.63137229, 11.22200409, 11.22200409, 11.81263588, 12.40326767, 
12.40326767, 12.40326767, 12.99389947, 13.58453126, 14.17516306, 
14.76579485, 15.94705844, 17.12832203, 17.71895382, 21.26274458, 
25.98779894, 63.19760196), action = c(0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L), 
    acnumber = c(137L, 137L, 137L, 137L, 137L, 137L, 137L, 137L, 
    137L, 137L, 137L, 137L, 137L, 137L, 137L, 137L, 137L, 137L, 
    137L, 137L, 137L), year = c(2018L, 2018L, 2018L, 2018L, 2018L, 
    2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 
    2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L)), .Names = c("SKU", 
"stuff", "action", "acnumber", "year"), class = "data.frame", row.names = c(NA, 
-21L))

action 列只有两个值 0 和 1。 正如我们所见,1 类的东西有 3 个观察值,0 类的东西有 18 个 obs。

我需要 -计算填充变量的中位数仅适用于类别 1(它等于 25.98779894),不带零。 正如我们所看到的,1 之间有零,它们需要被删除,如果存在负值,它们也需要被删除。 即,好像数据集是这样的:

structure(list(SKU = c(11202L, 11202L, 11202L, 11202L, 11202L, 
11202L, 11202L, 11202L, 11202L, 11202L, 11202L, 11202L, 11202L, 
11202L, 11202L, 11202L, 11202L, 11202L, 11202L, 11202L, 11202L
), stuff = c(8.85947691, 9.450108704, 10.0407405, 10.0407405, 
10.63137229, 11.22200409, 11.22200409, 11.81263588, 12.40326767, 
12.40326767, 12.40326767, 12.99389947, 13.58453126, 14.17516306, 
14.76579485, 15.94705844, 17.12832203, 17.71895382, 21.26274458, 
25.98779894, 63.19760196), action = c(0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 1L, NA, NA, NA, NA, NA, NA, NA, NA, 1L, 1L), 
    acnumber = c(137L, 137L, 137L, 137L, 137L, 137L, 137L, 137L, 
    137L, 137L, 137L, 137L, 137L, 137L, 137L, 137L, 137L, 137L, 
    137L, 137L, 137L), year = c(2018L, 2018L, 2018L, 2018L, 2018L, 
    2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 
    2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L)), .Names = c("SKU", 
"stuff", "action", "acnumber", "year"), class = "data.frame", row.names = c(NA, 
-21L))

我还需要通过 0 类的填充变量计算最后三个观察值的中位数,该变量在第一个之前, 在我们的例子中是 12,40326767

然后从类别 1 的中位数中减去类别 0 的中位数并乘以 1 的数量,在本例中为 3。

(25,98779894-12,40326767)*3=40,75359381

我该如何做这个操作?

我期望的输出

SKU     stuff     action    acnumber    year    value
11202   8,85947691  3          137      2018    40,75359381

【问题讨论】:

    标签: r dplyr subset lapply


    【解决方案1】:

    这是tidyverse 解决方案:

    df %>%
      group_by(SKU,acnumber,year) %>%
      summarize(value = 3*(median(stuff[action==1]) - median(stuff[match(1,action)-3:1])),
                stuff=first(stuff),
                action = sum(action)) %>%
      select(SKU,stuff,action,acnumber,year,value)
    
    # # A tibble: 1 x 6
    # # Groups:   SKU, acnumber [1]
    #     SKU stuff action acnumber  year value
    #   <int> <dbl>  <int>    <int> <int> <dbl>
    # 1 11202  8.86      3      137  2018  40.8
    

    【讨论】:

    • Moody_Mudskipper,非常非常好,但是你能帮我解决类似的问题,但按组分开。 stackoverflow.com/questions/50817958/…。如果可以回答这个话题,我只是删除新的相关帖子。
    • 我要去看看,但在我的回答中,我已经通过group_by 声明将它概括为几个组
    • Moody_Mudskipper,最后一个问题。根据 1 个类别的数据,必须计算中位数,不带负值或 0 值 (1,2,3,-4) -4 必须删除。怎么办?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多