Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- date <- seq(as.Date("2012-09-01"), Sys.Date(), 1)
- id <- rep(c("a","b","c","d"), 8)
- bdate <- seq(as.Date("2012-08-01"), as.Date("2012-11-01"), 1)[sample(1:32, 32)]
- # The end date should be random but greater than the begin date. However, I set it to 15 days later for simplicity.
- edate <- bdate + 15
- value <- seq(1, 1000, 1)[sample(1:1000, 32)]
- dfa <- data.frame(id, value, bdate, edate)
- names(dfa) <- c("ID", "Value", "Begin.Date", "End.Date")
- Date a b c
- 2012-08-01 XXX YYY ZZZ
- 2012-08-02 XXX YYY ZZZ
- 2012-08-03 XXX YYY ZZZ
- # Create additional data frame
- dfb <- data.frame(seq(as.Date("2012-08-01"), as.Date("2012-11-01"), 1))
- names(dfb)[1] <- "Date"
- # Variable for unique IDs
- nid <- unique(dfa$ID)
- # Number of total IDs
- tid <- length(nid)
- for (i in c(1:tid))
- {
- sums <- vapply(dfb$Date, function(x)
- {
- temp <- subset(dfa, dfa$ID == nid[i])
- temp <- subset(temp, temp$Begin.Date < x & temp$End.Date > x)
- res <- sum(temp$Value)
- res
- }, FUN.VALUE = 0.1
- )
- dfb[1+i] <- sums
- }
- # Change column names to ID
- names(dfb) <- c("Date", as.character(nid))
- mydate <- as.Date("2012-08-25") # take Aug 25, 2012 as an example
- ind <- (dfa$Begin.Date <= mydate) & (dfa$End.Date >= mydate)
- temp <- subset(dfa, ind)
- out <- table(temp$ID)
- library("plyr")
- library("reshape2")
- earliest.date <- as.Date("2007-01-01")
- latest.date <- as.Date("2012-11-01")
- dfa.long <- adply(dfa, 1, function(DF) {
- if(DF$End.Date >= earliest.date & DF$Begin.Date <= latest.date) {
- data.frame(Date=seq(max(DF$Begin.Date, earliest.date),
- min(DF$End.Date, latest.date),
- 1))
- }
- })
- dfb <- ddply(dfa.long, .(Date, ID), summarise, sum=sum(Value))
- dfb <- dcast(dfb, Date~ID, value.var="sum", fill=0)
Add Comment
Please, Sign In to add comment