Created
October 9, 2011 13:27
-
-
Save atomicules/1273684 to your computer and use it in GitHub Desktop.
Using data.table in R and taking the DT[J("a")] example at face value I ended up with a really slow function.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
duplicates.step2$days.between <- sapply( | |
1:nrow(duplicates.step2), | |
function(q) { | |
step2id <- as.character(duplicates.step2.dt[,id][q]) #as.character is important bit! | |
temp <- min( | |
difftime( | |
duplicates.step2.dt[, Date][q], | |
step1.dt[step2id]$Date, #No need to crazily create another data.table | |
units=c("days") | |
) | |
) | |
} | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
step1.dt <- data.table(step1) | |
setkey(step1, id) | |
duplicates.step2$days.between <- sapply( | |
1:nrow(duplicates.step2), #For each row in duplicates.step2 | |
function(q) { | |
step2id <- duplicates.step2.dt[,id][q] #Gets the id | |
temp <- min( | |
difftime( | |
duplicates.step2.dt[, Date][q], #Gets the date | |
step1.dt[J(eval(quote(step2id))]$Date, #gets the dates for all step1 rows of the same id. This is where it all goes wrong | |
units=c("days") | |
) | |
) | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment