Use the prob
argument of sample()
:
samp_idx <- sample(seq_len(nrow(data)), nsample, prob=data$ww)
new_data <- data[samp_idx, ]
Something like this. Running time is
# user system elapsed
# 0.015 0.000 0.014
versus your version:
# user system elapsed
# 4.278 0.007 4.290