When running the following code to do a block permutation (sampling without replacement, by groups), I routinely receive utterly bizarre results. Namely, when using sample on a block with only 1 element in it, instead of returning the original value, it will return a value NOT in the original vector.
Eg. I have received values 13, 29, etc. for block a below, which make no sense.
Any insight on what's happening in the sample() function to make this occur?
library(tidyverse)
data.frame(
weight = c(100, 200, 300, 200,
500, 300, 200, 100),
blocks = c("a", "b", "b", "b",
"c", "c", "c", "b")) %>%
group_by(blocks) %>%
mutate(perm = sample(weight, size = n(), replace = FALSE))