samedi 9 mai 2015

Permute/randomize rows within a column independently

I have a dataframe like so:

> df1
  a b c
1 0.5 0.3 0
2 0.2 0 0
3 0 0.6 0
4 0 0 0.4

I would like to permute the rows within each column with replacement 1000 times, however I would like to do this independently for each column (like a slot machine in Las Vegas).

I noticed that the sample function in R doesn't really allow this, for example sampling rowwise gives you.

> df2 <- df1[sample(nrow(df1)),]
> df2
  a b c
3 0 0.6 0
4 0 0 0.4
2 0.2 0 0
1 0.5 0.3 0

But notice how the whole row is taken as a chunk (i.e they are kept beside their columns e.g 0.5 is always beside 0.3)

I don't think doing this both column-wise and row-wise is the correct answer because then it is permuting horizontally and vertically (i.e not like a slot machine in Vegas).




Aucun commentaire:

Enregistrer un commentaire