Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using data.table in R and taking the DT[J("a")] example at face value I ended up with a really slow function.
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")
)
)
}
)
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
You can’t perform that action at this time.