How can I efficiently sample one row for each unique variable in a column from a datatable in R? For example, given the data.table:
library(data.table)
set.seed(1)
dt <- data.table( 
                   A = sample(c("A", "B", "C", "D", "E"), 100, replace = T),
                   B = sample(1:100, 100, replace = T),
                   C = sample(101:200, 100, replace = T) 
                 )
I need to sample one row for each unique character in column A. For example:
out <- list()
for (i in 1:length(unique(dt$A))){
  out[[i]] <- dt[sample(dt[, .I[A == unique(dt$A)[i]]], 1, replace = T)]
}
out <- do.call("rbind", out)
However, the data table I am applying this to is vary large. Is there a data.table method I can use to improve performance?