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


Here's one way:

df2 <- df1
n   <- nrow(df1)

set.seed(1)
df2[] <- lapply(df1,function(x) x[sample.int(n)] )
#     a   b   c
# 1 0.2 0.3 0.0
# 2 0.0 0.6 0.0
# 3 0.0 0.0 0.4
# 4 0.5 0.0 0.0

      



Or just lapply(df1,sample)

like @akrun said.

+3


source


The response parameters above return a list, which might be fine for your purposes. Here's another option:



set.seed(1)
matrix(sample((unlist(df1))), ncol = 3, dimnames = (list(NULL, letters[1:3])))

       a   b   c
[1,] 0.0 0.2 0.0
[2,] 0.3 0.6 0.5
[3,] 0.0 0.0 0.0
[4,] 0.0 0.4 0.0

      

0


source







All Articles