Do you have a faster way to resample (with replacement) groups in a dataset using R?
Edit: Note that I would like to resample groups of rows, not individual rows.
toydata <- data.frame(
group = rep(letters[1:3], each = 2),
rep = rep(1:2, times = 3),
value = 1:6)
print(toydata) group rep value 1 a 1 1 2 a 2 2 3 b 1 3 4 b 2 4 5 c 1 5 6 c 2 6
ngroups <- n_distinct(toydata$group)
nreps <- nrow(toydata) / ngroups
s <- sample(unique(toydata$group), replace = TRUE) # resampling groups with replacement
toydata_resampled <- left_join(
x = data.frame(group = rep(s, each = nreps), rep = rep(1:nreps, ngroups)),
y = toydata,
by = c("group", "rep"))
One expected output:
> print(toydata_resampled) group rep value 1 a 1 1 2 a 2 2 3 a 1 1 4 a 2 2 5 c 1 5 6 c 2 6