Imagine two datasets. Dataset df1 is the day of the year that a certain event occurred. Df1 has multiple other columns, but for the purpose of this question, only day matters. Df2 contains two columns, the day of the year a measurement was made, and the value of that measurement. Essentially, I want to create a new column ("AVG") in df1 that is the mean value of that day in df2 plus the value from the previous day. For example, the AVG for day 2 in df1 would be 12.5 ((10+15)/2).
Some example data below.
df1 <- structure(list(day = c(2, 5, 7)), class = "data.frame", row.names = c(NA,
-3L))
df2 <- structure(list(day = c(1, 2, 3, 4, 5, 6, 7, 8), value = c(10,
15, 8, 13, 7, 20, 25, 12)), class = "data.frame", row.names = c(NA,
-8L))
Example final product below.
df3 <- structure(list(day = c(2, 5, 7), AVG = c(12.5, 10, 22.5)), class = "data.frame", row.names = c(NA,
-3L))