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

android - Why am I getting the message 'Youractivity.java is not an activity subclass or alias' -

python - How do I create a list index that loops through integers in another list -

c# - “System.Security.Cryptography.CryptographicException: Keyset does not exist” when reading private key from remote machine -