I have a dataframe of names. And I have a vector of different food items. I want to pick one element from that vector randomly for each Name so that the data.table looks like be
We can use group by option and then do sample
sample
dt[, NextItem := sample(x, 1), by = Name]
Or you can also do this with .N instead of by
.N
by
dt[, NextItem := sample(x, .N, replace = TRUE)]