Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sample by group using the sample_n function of dplyr

Tags:

r

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?

like image 320
Jonas Tundo Avatar asked Jun 02 '14 13:06

Jonas Tundo


People also ask

What does Sample_n do in R?

sample_n() function in R Language is used to take random sample specimens from a data frame.

What does Slice_sample do in R?

It allows you to select, remove, and duplicate rows. It is accompanied by a number of helpers for common use cases: slice_head() and slice_tail() select the first or last rows. slice_sample() randomly selects rows.


1 Answers

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
like image 167
Garrett Mooney Avatar answered Sep 23 '22 21:09

Garrett Mooney