I have the following two data frames:

```
library(reshape2)

#y1
Date_from <- c("2013-01-01","2013-01-06")
Date_to <- c("2013-01-05","2013-01-08")
conc1<-c("1.5","2.5")
conc2<-c("2","3")
y1 <- data.frame(Date_from,Date_to,conc1,conc2)
y1$Date_from <- as.Date(y1$Date_from)
y1$Date_to <- as.Date(y1$Date_to)
y1<-melt(y1, id=c("Date_from","Date_to"))
y1$value<-as.numeric(y1$value)

#y2:
Date <- c("2013-01-01","2013-01-02","2013-01-03","2013-01-04","2013-01-05","2013-01-06","2013-01-07","2013-01-08")
conc1<-c("1.5","2.5","1.5","3.5","1.5","2.5","1.5","3.5")
conc2<-c("2.6","2.8","1.4","3.7","2.6","2.8","1.4","3.7")
y2 <- data.frame(Date,conc1,conc2)
y2$Date <- as.Date(y2$Date)

```

Y1 contains measured concentrations of two metals over a certain date range and y2 contains simulated numerical daily metal concentrations for those two metals. I will need to calculate the mean metal concentration in y2 based on the date ranges from y1 and add that to y1.

The result should look like this:

```
Date_from    Date_to     variable value  new_value
2013-01-01 2013-01-05    conc1     1.5    2.1
2013-01-06 2013-01-08    conc1     2.5    2.5
2013-01-01 2013-01-05    conc2     2.0    2.62
2013-01-06 2013-01-08    conc2     3.0    2.63

```

Where column "new\_value" contains the mean values of conc1 and conc2 from y2 for the respective date ranges. 

UPDATE:

I have tried the following with no success:

```
y2_melt<-melt(y2, id="Date")
y2_melt$value<-as.numeric(y2_melt$value)
helper <- merge(y2_melt,y1)
helper <- helper[helper$Date >= helper$Date_from & helper$Date <= helper$Date_to, ]
final1<-aggregate(helper$variable,
                     list(Date_from = helper$Date_from,
                          Date_to = helper$Date_to),
                          FUN="sum")
final2<-merge(final1,y2)
final3<-arrange(final2,Date_from,Date_to)

```