This is what my data.table/dataframe looks lke
library(data.table) dt <- fread(\' STATE ZIP PA 19333 PA 19327
Here's a way just using sample, wrapped up in a convenience function.
sample
sample_fill_na = function(x) { x_na = is.na(x) x[x_na] = sample(x[!x_na], size = sum(x_na), replace = TRUE) return(x) } dt[, ZIP := sample_fill_na(ZIP), by = STATE]