如果df
是你的數據幀,什麼下面確實是STARTDATE產生的所有月份的序列到結束日期,保持產品和宿舍的獨特組合,並計算平均值。
library(lubridate)
library(dplyr)
df <- df %>%
mutate(startdate = ymd(startdate),
enddate = ymd(enddate))
df$output <- mapply(function(x,y) seq(x, y, by = "month"),
df$startdate,
df$enddate)
df %>%
tidyr::unnest(output) %>%
mutate(quarter = paste0("Q",quarter(output), " ", year(output))) %>%
select(-output) %>%
group_by(product, startdate, enddate, quarter) %>%
filter(row_number(quarter) == 1) %>%
summarise(mean(price))
結果爲您的數據幀的第一行是:
product startdate enddate quarter `mean(price)`
<int> <date> <date> <chr> <dbl>
1 1 2012-03-17 2016-09-08 Q1 2012 10
2 1 2012-03-17 2016-09-08 Q1 2013 10
3 1 2012-03-17 2016-09-08 Q1 2014 10
4 1 2012-03-17 2016-09-08 Q1 2015 10
5 1 2012-03-17 2016-09-08 Q1 2016 10
6 1 2012-03-17 2016-09-08 Q2 2012 10
7 1 2012-03-17 2016-09-08 Q2 2013 10
8 1 2012-03-17 2016-09-08 Q2 2014 10
9 1 2012-03-17 2016-09-08 Q2 2015 10
10 1 2012-03-17 2016-09-08 Q2 2016 10
11 1 2012-03-17 2016-09-08 Q3 2012 10
12 1 2012-03-17 2016-09-08 Q3 2013 10
13 1 2012-03-17 2016-09-08 Q3 2014 10
14 1 2012-03-17 2016-09-08 Q3 2015 10
15 1 2012-03-17 2016-09-08 Q3 2016 10
16 1 2012-03-17 2016-09-08 Q4 2012 10
17 1 2012-03-17 2016-09-08 Q4 2013 10
18 1 2012-03-17 2016-09-08 Q4 2014 10
19 1 2012-03-17 2016-09-08 Q4 2015 10