Permutation test in R for a bivariate Pearson correlation

The permutation test recommended by Bishara and Hittner (2012) for samples of under 10 for testing if a correlation is zero can be obtained in R. This uses an approach which randomly permutes the values of one of the variables being correlated (e.g. b) to compare with the observed correlation. This corresponds to sampling without replacement (see here). Note that this is not an 'exact' test which would use exactly all n! permutations of b to produce correelations to compare with the observed correlation (see for example here).

Suppose we have two columns of data

a <- c(1,2,3)
b <- c(3,6,5)

then the two-sided p-value testing if the a,b Pearson correlation equals zero may be outputted in pout (assuming 1000 simulations) using the below.

robs <- cor(a,b)
repout <- replicate(1000,cor(a,sample(b)))
rout<-(abs(repout)>robs)
pout<-sum(rout=="TRUE")/1000

The 2.5 and 97.5 percentiles of 'repout' computed as above provide a 95% Confidence Interval for the permuted correlation.