Suppose I\'ve got the following data.table :
data.table
dt <- data.table(id=c(1,1,1,1,1,1,2,2,2,2), wday=c(\"mon\",\"tue\",\"wed\",\"thu\",\"
One other possilibity with match and ddply :
FUN <- function(x) { y <- x$val[match(c("mon", "tue", "wed", "thu", "fri", "sat", "sun"), x$wday, nomatch=NA)] y[is.na(y)] <- 0 y <- data.frame(wday=c("mon", "tue", "wed", "thu", "fri", "sat", "sun"), val=y) y } ddply(dt, .(id), FUN)