Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
400 views
in Technique[技术] by (71.8m points)

r - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...