Jonas Tundo
Jonas Tundo

Reputation: 6197

Sample by group using the sample_n function of dplyr

According to the dplyr help file the sample_n function samples a fixed number per group.

When I run the following code I expect two samples per tobgp and alcgp combination, so 32 (4*4*2) lines in total. However only two lines are returned.

by_tobgp_alcgp <- esoph %>% group_by(tobgp,alcgp)

sample_n(by_tobgp_alcgp , 2)


# Source: local data frame [2 x 5]
# Groups: tobgp, alcgp
# 
#    agegp     alcgp tobgp ncases ncontrols
# 10 25-34    80-119 10-19      0         1
# 50 55-64 0-39g/day   30+      4         6

Is this correct? Is there an alternative way to achieve this using dplyr?

Upvotes: 9

Views: 8627

Answers (2)

Garrett Mooney
Garrett Mooney

Reputation: 184

The issue that @Henrik described has been closed.

The sample_n operator will work on grouped data frames for dplyr versions >= 0.3

library(dplyr)
data(esoph)

set.seed(123)

esoph %>%
  group_by(tobgp, alcgp) %>%
  sample_n(2)

#Source: local data frame [32 x 5]
#Groups: tobgp, alcgp [16]
#
#agegp     alcgp    tobgp ncases ncontrols
#(fctr)    (fctr)   (fctr)  (dbl)     (dbl)
#1   65-74 0-39g/day 0-9g/day      5        48
#2   25-34 0-39g/day 0-9g/day      0        40
#3   45-54     40-79 0-9g/day      6        38
#4     75+     40-79 0-9g/day      2         5
#5   55-64    80-119 0-9g/day      9        18
#6   35-44    80-119 0-9g/day      0        11
#7   45-54      120+ 0-9g/day      4         4
#8   65-74      120+ 0-9g/day      3         4
#9   45-54 0-39g/day    10-19      0        18
#10  65-74 0-39g/day    10-19      4        14
#11    75+     40-79    10-19      1         3
#12  55-64     40-79    10-19      6        21
#13  45-54    80-119    10-19      6        14
#14  25-34    80-119    10-19      0         1
#15    75+      120+    10-19      1         1
#16  35-44      120+    10-19      0         3
#17  25-34 0-39g/day    20-29      0         6
#18  55-64 0-39g/day    20-29      3        12
#19  65-74     40-79    20-29      5         9
#20  25-34     40-79    20-29      0         4
#21  55-64    80-119    20-29      3         6
#22  65-74    80-119    20-29      2         3
#23  45-54      120+    20-29      2         3
#24  35-44      120+    20-29      2         4
#25  55-64 0-39g/day      30+      4         6
#26  35-44 0-39g/day      30+      0         8
#27  35-44     40-79      30+      0         8
#28  25-34     40-79      30+      0         7
#29  35-44    80-119      30+      0         1
#30  55-64    80-119      30+      4         4
#31  25-34      120+      30+      0         2
#32  65-74      120+      30+      1         1

Upvotes: 9

talat
talat

Reputation: 70256

You can work around this issue by using the do operator:

library(dplyr)
data(esoph)

esoph %>%
  group_by(tobgp, alcgp) %>%
  do(sample_n(., 2))

#   agegp     alcgp    tobgp ncases ncontrols
#1    75+ 0-39g/day 0-9g/day      1        18
#2  35-44 0-39g/day 0-9g/day      0        60
#3  55-64     40-79 0-9g/day      9        40
#4    75+     40-79 0-9g/day      2         5
#5  65-74    80-119 0-9g/day      6        13
#6  55-64    80-119 0-9g/day      9        18
#7  65-74      120+ 0-9g/day      3         4
#8  25-34      120+ 0-9g/day      0         1
#9  25-34 0-39g/day    10-19      0        10
#10 35-44 0-39g/day    10-19      1        14
#11 65-74     40-79    10-19      3        10
#12 55-64     40-79    10-19      6        21
#13 55-64    80-119    10-19      8        15
#14 35-44    80-119    10-19      0         6
#15 25-34      120+    10-19      1         1
#16 35-44      120+    10-19      0         3
#17 25-34 0-39g/day    20-29      0         6
#18 35-44 0-39g/day    20-29      0         7
#19 45-54     40-79    20-29      5        15
#20   75+     40-79    20-29      0         3
#21 65-74    80-119    20-29      2         3
#22 45-54    80-119    20-29      1         5
#23 55-64      120+    20-29      2         3
#24 45-54      120+    20-29      2         3
#25 25-34 0-39g/day      30+      0         5
#26 55-64 0-39g/day      30+      4         6
#27 25-34     40-79      30+      0         7
#28   75+     40-79      30+      1         1
#29 55-64    80-119      30+      4         4
#30 35-44    80-119      30+      0         1
#31 55-64      120+      30+      5         6
#32 25-34      120+      30+      0         2

Edit after comment:

For group data, the do operator applies a function to each group of data which is what is wanted in this case (select a sample of size 2 of each group). So while sample_n doesn't work on grouped data (not sure if it is supposed to, but I guess it should work), using do(sample_n(data,n)) does the job as desired.

Upvotes: 5

Related Questions