I ran into a bit of an odd one and I didn't know where to address it so it seemed most appropriate here. Essentially, I wanted to write the below code as setdiff(door,cardoor) %>% sample(,1) but it provides two numbers instead of one as should be the case with size = 1. The only way I can guarantee this output is if I specify size = 1 argument explicitly yet it is fine with the last one even though it is not written explicitly.

Any idea why the piped version does not work in the same way as the last one? All three these should technically be equivalent?

I am guessing with the sample(,1) notation that the magrittr pipe pushed the the piped argument into the first argument moving each positional argument one further down. So c('a', 'b') %>% sample(,1) becomes shorthand for sample(c('a', 'b'), , 1), which is sample(c('a', 'b'), replace = 1) (replace being the 3rd positional argument of sample()). I would suggest not using the "extra comma: notation and trying c('a', 'b') %>% sample(1).

In addition why sample is willing to run without the size argument set is strange.

For sample the default for size is the number of items inferred from the first argument, so that sample(x) generates a random permutation of the elements of x (or 1:x ).

So, if replace is supplied as 1, it's considered as TRUE and a with replacement sample of same size as the x argument is generated.