plyr, . , (.. ), .
df <-
structure(list(User = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L), Morning = c(NA, 50L, 85L, 62L, 48L, 19L, 25L, NA, 48L,
28L, 35L, 91L), Evening = c(NA, 115, 128, NA, 100.8, 71, 98,
105, 105, 203, 80.99, 78.25), Measure_Date = structure(c(1L,
2L, 3L, 5L, 9L, 10L, 6L, 7L, 1L, 2L, 4L, 8L), .Label = c("2/18/11",
"2/19/11", "2/20/11", "2/21/11", "2/25/11", "3/10/11", "3/11/11",
"3/14/11", "3/8/11", "3/9/11"), class = "factor")), .Names = c("User",
"Morning", "Evening", "Measure_Date"), class = "data.frame", row.names = c(NA,
-12L))
df$Measure_Date <- as.Date(df$Measure_Date, format='%m/%d/%y')
library(package=plyr)
ddply(.data=df, .variables='User',
.fun=function(x){
tdf <- data.frame(Measure_Date=seq(from=min(x$Measure_Date),
to=max(x$Measure_Date),
by='1 day'))
tdf <- merge(tdf, x, all=TRUE)
tdf$Evening <- c(NA, tdf$Evening[-length(tdf$Evening)])
tdf$Difference <- tdf$Evening - tdf$Morning
tdf <- tdf[,c('Measure_Date', 'Difference')]
x <- merge(x, tdf)
x
})