如何在R中分别计算组的中位数

时间:2018-06-19 17:07:33

标签: r dplyr plyr lapply

数据小例子

    df=structure(list(Dt = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 
22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 
35L, 36L, 37L, 38L, 39L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 
23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 
36L, 37L, 38L, 39L), .Label = c("2018-02-20 00:00:00.000", "2018-02-21 00:00:00.000", 
"2018-02-22 00:00:00.000", "2018-02-23 00:00:00.000", "2018-02-24 00:00:00.000", 
"2018-02-25 00:00:00.000", "2018-02-26 00:00:00.000", "2018-02-27 00:00:00.000", 
"2018-02-28 00:00:00.000", "2018-03-01 00:00:00.000", "2018-03-02 00:00:00.000", 
"2018-03-03 00:00:00.000", "2018-03-04 00:00:00.000", "2018-03-05 00:00:00.000", 
"2018-03-06 00:00:00.000", "2018-03-07 00:00:00.000", "2018-03-08 00:00:00.000", 
"2018-03-09 00:00:00.000", "2018-03-10 00:00:00.000", "2018-03-11 00:00:00.000", 
"2018-03-12 00:00:00.000", "2018-03-13 00:00:00.000", "2018-03-14 00:00:00.000", 
"2018-03-15 00:00:00.000", "2018-03-16 00:00:00.000", "2018-03-17 00:00:00.000", 
"2018-03-18 00:00:00.000", "2018-03-19 00:00:00.000", "2018-03-20 00:00:00.000", 
"2018-03-21 00:00:00.000", "2018-03-22 00:00:00.000", "2018-03-23 00:00:00.000", 
"2018-03-24 00:00:00.000", "2018-03-25 00:00:00.000", "2018-03-26 00:00:00.000", 
"2018-03-27 00:00:00.000", "2018-03-28 00:00:00.000", "2018-03-29 00:00:00.000", 
"2018-03-30 00:00:00.000"), class = "factor"), ItemRelation = c(158043L, 
158043L, 158043L, 158043L, 158043L, 158043L, 158043L, 158043L, 
158043L, 158043L, 158043L, 158043L, 158043L, 158043L, 158043L, 
158043L, 158043L, 158043L, 158043L, 158043L, 158043L, 158043L, 
158043L, 158043L, 158043L, 158043L, 158043L, 158043L, 158043L, 
158043L, 158043L, 158043L, 158043L, 158043L, 158043L, 158043L, 
158043L, 158043L, 158043L, 234L, 234L, 234L, 234L, 234L, 234L, 
234L, 234L, 234L, 234L, 234L, 234L, 234L, 234L, 234L, 234L, 234L, 
234L, 234L, 234L, 234L, 234L, 234L, 234L, 234L, 234L, 234L, 234L, 
234L, 234L, 234L, 234L, 234L, 234L, 234L, 234L, 234L, 234L, 234L
), stuff = c(200L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 3600L, 
0L, 0L, 0L, 0L, 700L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 1000L, 2600L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 400L, 700L, 
200L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 3600L, 0L, 0L, 0L, 
0L, 700L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1000L, 
2600L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 400L, 700L), num = c(1459L, 
1459L, 1459L, 1459L, 1459L, 1459L, 1459L, 1459L, 1459L, 1459L, 
1459L, 1459L, 1459L, 1459L, 1459L, 1459L, 1459L, 1459L, 1459L, 
1459L, 1459L, 1459L, 1459L, 1459L, 1459L, 1459L, 1459L, 1459L, 
1459L, 1459L, 1459L, 1459L, 1459L, 1459L, 1459L, 1459L, 1459L, 
1459L, 1459L, 1459L, 1459L, 1459L, 1459L, 1459L, 1459L, 1459L, 
1459L, 1459L, 1459L, 1459L, 1459L, 1459L, 1459L, 1459L, 1459L, 
1459L, 1459L, 1459L, 1459L, 1459L, 1459L, 1459L, 1459L, 1459L, 
1459L, 1459L, 1459L, 1459L, 1459L, 1459L, 1459L, 1459L, 1459L, 
1459L, 1459L, 1459L, 1459L, 1459L), year = c(2018L, 2018L, 2018L, 
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 
2018L, 2018L, 2018L), action = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 1L, 1L, 1L, 1L)), .Names = c("Dt", "ItemRelation", 
"stuff", "num", "year", "action"), class = "data.frame", row.names = c(NA, 
-78L))
现在每个小组 ItemRelation + num + year我必须计算中位数。 如果我使用此解决方案

# df with action 0 and stuff > 0
v <- df$stuff[intersect(which(df$action == 0),
                        which(df$stuff > 0))]

# df with action 1 and stuff > 0
w <- df$stuff[intersect(which(df$action == 1),
                        which(df$stuff > 0))]


# calulating the median of v for the last 5 observations
l <- length(v)
m0 <- median(v[(l-4):l]) # taking the median of the last 5 observations
# computing the final difference
m <- median(w) - m0

我一次计算所有组的中位数,但我必须计算 每个组分别。 我该如何执行?

此处为预期输出

ItemRelation    num year    value
158043       1459   2018    45
158043      234    2018     67

帖子已编辑。请注意,值不是真实的,中位数会是另一个,我只想显示输出结果

编辑

动作列只有两个值0和1。我必须使用1个类别之前的最后五个整数值来计算1个类别的动作的中值,然后是零类别的动作的中值。我只接受最后5个观察值,有必要采取零作用类别中的最后5个观察结果,但仅取整数值,而不是按零类别所有值计算中位数。在我们的例子中是

200
3600
700
1000
2600

然后从一个类别的中位数减去零类别的中位数。

在零操作类别中按事物进行观察的次数可以在0到10之间变化。如果我们有10个零类别的整数,则取最后五个。如果只有1,2,3,4,5个整数值,我们减去整数的实数的中位数。如果我们只有0个而没有整数,那么我们就等于0。

但是代码必须按零类别计算中位数,但必须在一个类别之前计算5个最后一个obs。

请注意,零操作类别可能有其他值,而不是0。

2 个答案:

答案 0 :(得分:2)

最简单的方法是使用group_by包中的summarizedplyr

library(dplyr)

# median of groups
medians <- df %>%
    group_by(ItemRelation, num, year) %>%
    summarize(med = median(stuff, na.rm = T))

# median of nonzero values in each group
medians <- df %>%
    filter(stuff>0) %>%
    group_by(ItemRelation, num, year) %>%
    summarize(med = median(stuff, na.rm = T))


subtract <- function(x){return(x[1]-x[2])}
median_diffs <- medians %>%
                group_by(ItemRelation, num, year) %>%
                mutate(med_diff = subtract(med))

答案 1 :(得分:2)

可以使用dplyr并遵循以下提到的步骤来实现一种解决方案。请在下面的代码中找到有关方法的注释。

注意:看来,OP中的样本数据本身并不是很有意义。

library(dplyr)

df %>% filter(stuff > 0) %>%  #First filter out for stuff > 0 which of our interest
  group_by(ItemRelation, num, year) %>%
    mutate(m = median(stuff[action==1]),
           m0 = median(tail(stuff[action==0], 5))) %>%  # Calculate m and m0 for all rows
  filter(action == 1) %>%  # Now keep only rows with action == 1
  mutate(m = m-m0) %>%
  select(-Dt,-m0,-action)

# # A tibble: 4 x 5
# # Groups: ItemRelation, num, year [2]
# ItemRelation stuff   num  year     m
# <int> <int> <int> <int> <dbl>
# 1       158043   400  1459  2018  -450
# 2       158043   700  1459  2018  -450
# 3          234   400  1459  2018  -450
# 4          234   700  1459  2018  -450