Here is one attempt at a speed up:
Day <- unique(dt$Week_Day)
setkey(dt, Week_Day)
# create columns of 0s
dt[, (Day) := 0L]
for (i in seq_along(head(Day, -1))) {
dt[Day[i], Day[i] := 1L]
}
This implements a couple of the data.table
speed ups including binary search in the second chain and the elimination of ifelse
with replacement by reference.