数据整理-R-想要删除在Fyweek和Launchweek之间对应的行

时间:2018-11-06 05:03:04

标签: r dataframe data-science data-manipulation

寻找一种解决方案,以删除FY_Week和Launch_Week之间几周的行。每行都代表一个product_ID,如下表所示:-

目标是-不考虑以黄色阴影显示的行(属于fy_week和发布周之间的几周的记录)。并且我们希望汇总相同的平均销售额。在r中寻找解决方案。提前致谢 :) Dataset

1 个答案:

答案 0 :(得分:0)

以下是使用dplyr的答案。

library(dplyr)

重新创建数据:

data <- tibble(
  product_id = rep(c("abc1", "abc2"), each = 3),
  FYWeek = rep(c(20, 22), each = 3),
  Launch_Week = rep(c(30, 28), each = 3),
  Week = c(21, 31, 32, 25, 29, 26),
  Sale = c(453, 408, 473, 466, 473, 412)
)

丢弃不符合您条件的行,并获取剩余行Sale的平均值:

data %>% 
  filter(Week < FYWeek | Week > Launch_Week) %>%
  mutate(avg = mean(Sale))

# A tibble: 3 x 6
  product_id FYWeek Launch_Week  Week  Sale   avg
  <chr>       <dbl>       <dbl> <dbl> <dbl> <dbl>
1 abc1           20          30    31   408  451.
2 abc1           20          30    32   473  451.
3 abc2           22          28    29   473  451.