user96368
user96368

Reputation: 83

How to sample a dataframe column within a group without reordering of rows?

I have a R dataframe d2 with two columns i.e., "class" and "entry". Where "class" is categorical variable and "entry" is continuous given by

d2

   class entry
1      1     1
2      2     2
3      3     3
4      4     4
5      1     5
6      2     6
7      3     7
8      4     8
9      1     9
10     2    10
11     3    11
12     4    12
13     1    13
14     2    14
15     3    15
16     4    16
17     1    17
18     2    18
19     3    19
20     4    20

I Now want to sample "entry" column within the a group given as

ddply(d2,.(class),function(x) x[sample(nrow(x)),])

   class entry
1      1     9
2      1     1
3      1     5
4      1    13
5      1    17
6      2    14
7      2    10
8      2     2
9      2     6
10     2    18
11     3    15
12     3    19
13     3     7
14     3     3
15     3    11
16     4    16
17     4    20
18     4    12
19     4     8
20     4     4

It is clearly visible that desired results are obtained but order of the "class" variable is also changed compared to original data d2. I want to get same output without reordering rows of "class" variable of d2. Thanks

Upvotes: 1

Views: 45

Answers (1)

akrun
akrun

Reputation: 887851

Here is one option with data.table

library(data.table)
setDT(d2)[, entry := sample(entry), by = class]

or use ave from base R

d2$entry <- with(d2, ave(entry, class, FUN = sample))

Upvotes: 1

Related Questions