Shuffle the columns of a matrix indepently using apply or plyr
apply, plyr, r, sample
Solution
How about this?
> apply(A, 2, sample)
Problem
I have a matrix in R, and without the need of doing a for loop I'd like to randomly shuffle the rows, but for each column independently. I.e., ``` A=cbind(c(1,2,3),c(4,5,6)) ``` and I'd like to have a new matrix, let us say A.shuffle where column 1 c(1,2,3) and column 2 c(4,5,6), are randomly shuffled independently. For example, ``` A.shuffle = cbind(c(1,3,2), c(6,4,5)) ``` i.e., in column 1 the random shuffle changed the position of 2 and 3, and in column 2 a different random shuffle changed the position of 6 to 4, 4 to 5 and 5 to 6. What'd be a good way to do that in R without the need of doing for loops? Thanks!