使用dplyr从数据框中抽取子组的行 [英] sample rows of subgroups from dataframe with dplyr

查看:214
本文介绍了使用dplyr从数据框中抽取子组的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

If I want to randomly select some samples from different groups I use the plyr package and the code below

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

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

从每个物种中选择10个样品。

Here 10 samples are selected from each species.

我的一些数据框非常大,我的问题是可以使用与dplyr包相同的sampleGroup函数吗?或者还有另一种方式在dplyr中做同样的事情?

Some of my dataframes are very big and my question is can I use the same sampleGroup function with the dplyr package? Or is there another way to do the same in dplyr?

编辑

dplyr包的版本0.2引入了两个新功能,从表sample_n和sample_frac中选择随机行$ / b $ b

Version 0.2 of the dplyr package introduced two new functions to select random rows from a table sample_n and sample_frac

推荐答案

是的,您可以使用dlyr优雅的功能do()。
这是一个例子:

Yes, you can use dplyr elegantly by the function do(). Here is an example:

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

,结果如下



and the results are like this

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中, sample_n 不再需要 do 函数。当前采用每组两行随机抽样的代码:

The do function is no longer needed for sample_n in newer versions of dplyr. Current code for taking a random sample of two rows per group:

mtcars %>% 
    group_by(cyl) %>% 
    sample_n(2)

这篇关于使用dplyr从数据框中抽取子组的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆