Setdiff() sample() oddity



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?


door <- c(1,2,3)
cardoor <- 1

setdiff(door,cardoor) %>% sample(,size = 1)
setdiff(door,cardoor) %>% sample(,1)

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.

I'm making a guess that this is the reason.

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.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.