scramble <- function(x) x[sample(nrow(x)), ] subscramble <- function(x, condition) { scramble(subset(x, condition)) } subscramble(mtcars, cyl == 4)