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 below.
dt <- fread('
We can use group by option and then do
dt[, NextItem := sample(x, 1), by = Name]
Or you can also do this with
.N instead of
dt[, NextItem := sample(x, .N, replace = TRUE)]