31

如果我想从不同组中随机选择一些样本,我使用 plyr 包和下面的代码

require(plyr)
sampleGroup<-function(df,size) {
  df[sample(nrow(df),size=size),]
}

iris.sample<-ddply(iris,.(Species),function(df) sampleGroup(df,10))

这里从每个物种中选择 10 个样本。

我的一些数据框非常大,我的问题是我可以使用与 dplyr 包相同的 sampleGroup 函数吗?还是在 dplyr 中有另一种方法可以做到这一点?

编辑

dplyr 包的 0.2 版引入了两个新函数,用于从表 sample_n 和 sample_frac 中选择随机行

4

4 回答 4

63

是的,您可以通过函数 do() 优雅地使用 dplyr。这是一个例子:

mtcars %>% 
    group_by(cyl) %>%
    do(sample_n(.,2))

结果是这样的

Source: local data frame [6 x 11]
Groups: cyl

   mpg cyl  disp  hp drat    wt  qsec vs am gear carb
1 24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
2 26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
3 21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
4 17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
5 14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
6 15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8

更新:

在较新版本的 dplyr 中do不再需要该功能。sample_n当前用于每组随机抽取两行样本的代码:

mtcars %>% 
    group_by(cyl) %>% 
    sample_n(2)
于 2014-04-21T07:48:56.257 回答
10

使用 data.table 很容易做到这一点,并且对大表很有用。

注意: 正如特洛伊在评论中提到的,使用 data.table 有一种更有效的方法,但我想在答案中尊重 OP 示例函数和格式。

require(data.table)
DT <- data.table(x = rnorm(10e6, 100, 50), y = letters)

sampleGroup<-function(df,size) {
  df[sample(nrow(df),size=size),]
}

result <- DT[, sampleGroup(.SD, 10), by=y]
print(result)

# y         x y
# 1: a  30.11659 m
# 2: a  57.99974 h
# 3: a  58.13634 o
# 4: a  87.28466 x
# 5: a  85.54986 j
# ---              
# 256: z 149.85817 d
# 257: z 160.24293 e
# 258: z  26.63071 j
# 259: z  17.00083 t
# 260: z 130.27796 f

system.time(DT[, sampleGroup(.SD, 10), by=y])
# user  system elapsed 
# 0.66    0.02    0.69 

Using the iris dataset:
iris <- data.table(iris)
iris[,sampleGroup(.SD, 10), by=Species]
于 2014-01-21T12:16:34.180 回答
7

这是个好问题!用文档记录的语法看不到任何简单dplyr的方法,但是如何解决这个问题?

sampleGroup<-function(df,x=1){

  df[
    unlist(lapply(attr((df),"indices"),function(r)sample(r,min(length(r),x))))
    ,]

}

sampleGroup(iris %.% group_by(Species),3)

#Source: local data frame [9 x 5]
#Groups: Species
#
#    Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
#39           4.4         3.0          1.3         0.2     setosa
#16           5.7         4.4          1.5         0.4     setosa
#25           4.8         3.4          1.9         0.2     setosa
#51           7.0         3.2          4.7         1.4 versicolor
#62           5.9         3.0          4.2         1.5 versicolor
#59           6.6         2.9          4.6         1.3 versicolor
#148          6.5         3.0          5.2         2.0  virginica
#103          7.1         3.0          5.9         2.1  virginica
#120          6.0         2.2          5.0         1.5  virginica

编辑 - 性能比较

这是针对 1m 行、26 个组使用 data.table(本机和根据示例使用函数调用)的测试。

本机 data.table 的速度大约是 dplyr 解决方法的 2 倍,也比带有标注的 data.table 调用快 2 倍。所以可能 dplyr / data.table 的性能大致相同。

希望 dplyr 的家伙很快会给我们一些采样的原生语法!(甚至更好,也许它已经存在了)

sampleGroup.dt<-function(df,size) {
  df[sample(nrow(df),size=size),]
}

testdata<-data.frame(group=sample(letters,10e5,T),runif(10e5))

dti<-data.table(testdata)

# using the dplyr workaround with external function call
system.time(sampleGroup(testdata %.% group_by(group),10))
#user  system elapsed 
#0.07    0.00    0.06 

#using native data.table
system.time(dti[dti[,list(val=sample(.I,10)),by="group"]$val])
#user  system elapsed 
#0.04    0.00    0.03 

#using data.table with external function call
system.time(dti[, sampleGroup.dt(dti, 10), by=group])
#user  system elapsed 
#0.06    0.02    0.08 
于 2014-01-21T12:22:50.867 回答
2

Dplyr 1.0.2 现在可以使用各种动词进行子集:https ://dplyr.tidyverse.org/reference/slice.html 包括随机 slice_sample:

mtcars %>% 
  slice_sample(n = 10)

并添加 group by 以按类别采样:

mtcars %>% 
  group_by(cyl) %>% 
  slice_sample(n = 2)
于 2020-12-04T20:30:43.413 回答