r - Sum counts given in one data.frame up to dates given in another data.frame -


i update audit data frame comparisons within data frame:

test: data frame of number of patients date:

test <- data.frame(         date=as.date(c("2012-12-12","2012-12-29","2013-01-15")),         patients=c(4,7,3) )         date patients 1 2012-12-12        4 2 2012-12-29        7 3 2013-01-15        3 

audit: data frame of audit dates:

audit <- data.frame(date=as.date(c("2012-12-31","2013-01-31")),          count=c(na))         date count 1 2012-12-31    na 2 2013-01-31    na 

q: audit$count should total number of patients in test audit$date?

this doesn't work:

audit$count <- sum(test[test$date < audit$date,]$patients) 

how best it?

one way use sapply values of audit$date:

audit$count <- sapply(audit$date, function(x) sum(test$patients[test$date < x]))          date count 1 2012-12-31    11 2 2013-01-31    14 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -