scramble <-function(x) x[sample(nrow(x)),]
subscramble <-function(x, condition){
scramble(subset(x, condition))}
subscramble(mtcars, cyl ==4)
This returns the error:
Error in eval(expr, envir, enclos) : object 'cyl' not found
because R no longer "knows" where to find the object called 'cyl'. He also points out the truly bizarre stuff that can happen if by chance there is an object called 'cyl' in the global environment:
cyl <-4
subscramble(mtcars, cyl ==4)
cyl <- sample(10,100, rep = T)
subscramble(mtcars, cyl ==4)