jeudi 25 mai 2017

sample each two columns together in a data frame in R

I have a very large data frame that contains 100 rows and 400000 columns.

To sample each column, I can simply do:

df <- apply(df, 2, sample)

But I want every two column to be sampled together. For example, if originally col1 is c(1,2,3,4,5) and col2 is also c(6,7,8,9,10), and after resampling, col1 becomes c(1,3,2,4,5), I want col2 to be c(6,8,7,9,10) that follows the resampling pattern of col1. Same thing for col3 & col4, col5 & col6, etc.

I wrote a for loop to do this, which takes forever. Is there a better way? Thanks!




Aucun commentaire:

Enregistrer un commentaire