Redirect / randomize rows within a column independently
I have a data clock:
> 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 rearrange the rows in each column with the replacement 1000 times, however I would like to do it myself for each column (for example, in a Las Vegas slot machine).
I've noticed that the sampling function in R doesn't actually allow this, for example sampling does.
> 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 stored next to their columns, for example 0.5 is always next to 0.3)
I don't think this is both column-wise and row-wise - the correct answer, because then it rearranges horizontally and vertically (i.e. not like a Vegas slot machine).
+3
source to share
2 answers