Q:

Calculate average for specific date range in R

I have two data frames:
Date <- seq(as.Date("2013/1/1"), by = "day", length.out = 17)
x <-data.frame(Date)
x$discharge <- c("1000","1100","1200","1300","1400","1200","1300","1300","1200","1100","1200","1200","1100","1400","1200","1100","1400")
x$discharge <- as.numeric(x$discharge)

And:
Date2 <- c("2013-01-01","2013-01-08","2013-01-12","2013-01-17")
y <- data.frame(Date2)
y$concentration <- c("1.5","2.5","1.5","3.5")
y$Date2 <- as.Date(y$Date2)
y$concentration <- as.numeric(y$concentration)

What I am desperately trying to do is to the following: 

In data frame y the first measurement is for the period 2013-01-01 to 2013-01-07
Calculate the average discharge for this period in data frame x
Return the average discharge to data frame y in a new column next to the first measurement and continue with the next measurement

I was having a look into function such as dplyr or apply but was not able to figure it out.

A:

library(dplyr)
x %>% 
  mutate(period = cut(as.Date(Date), c(as.Date("1900-01-01"), as.Date(y$Date2[-1]), as.Date("2100-01-01")), c(1:length(y$Date2)))) %>% 
  group_by(period) %>% 
  mutate(meandischarge = mean(discharge, na.rm = T)) %>% 
  right_join(y, by = c("Date" = "Date2"))

        Date discharge period meandischarge concentration
      <date>     <dbl> <fctr>         <dbl>         <dbl>
1 2013-01-01      1000      1      1214.286           1.5
2 2013-01-08      1300      2      1200.000           2.5
3 2013-01-12      1200      3      1200.000           1.5
4 2013-01-17      1400      4      1400.000           3.5

If you only want the original y variables, you could do this:
x %>% 
  mutate(period = cut(as.Date(Date), c(as.Date("1900-01-01"), as.Date(y$Date2[-1]), as.Date("2100-01-01")), c(1:length(y$Date2)))) %>% 
  group_by(period) %>% 
  mutate(meandischarge = mean(discharge, na.rm = T)) %>% 
  ungroup() %>% 
  right_join(y, by = c("Date" = "Date2")) %>% 
  select(Date2 = Date, concentration, meandischarge)
       Date2 concentration meandischarge
      <date>         <dbl>         <dbl>
1 2013-01-01           1.5      1214.286
2 2013-01-08           2.5      1200.000
3 2013-01-12           1.5      1200.000
4 2013-01-17           3.5      1400.000

