Skip to content

Instantly share code, notes, and snippets.

@atomicules
Created October 9, 2011 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atomicules/1273684 to your computer and use it in GitHub Desktop.
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.
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