dplyr sample_n其中n是分组变量的值 [英] dplyr sample_n where n is the value of a grouped variable

查看:279
本文介绍了dplyr sample_n其中n是分组变量的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下分组的数据框,我想使用函数 dplyr :: sample_n 从每个组的数据框架中提取行。我想使用每个组中分组变量 NDG 的值作为从每个组中提取的行数。

 > dg.tmp<  -  structure(list(Gene = c(CAMK1,GHRL,TIMP4,CAMK1,GHRL,
TIMP4,ARL8B,ARPC4 SEC13,ARL8B,ARPC4,SEC13,
),GLB = c(3,3,3,3,3,10,10,10,10,10,10) NDG = c(1,
1,1,2,2,2,1,1,2,2,2,2)),class = c(tbl_df,tbl,
data.frame),row.names = c(NA,-12L),.Names = c(Gene,GLB,
NDG))

> ; dg< - dg.tmp%>%
dplyr :: group_by(GLB,NDG)

> dg
资料来源:本地数据框架[12 x 3]
组:GLB,NDG

基因GLB NDG
1 A4GNT 3 1
2 ABTB1 3 1
3 AHSG 3 1
4 A4GNT 3 2
5 ABTB1 3 2
6 AHSG 3 2
7 AADAC 10 1
8 ABHD14B 10 1
9 ACVR2B 10 1
10 AADAC 10 2
11 ABHD14B 10 2
12 ACVR2B 10 2

例如,假设正确的随机选择,我想要代码

 > dg%>%dplyr :: sample_n(NDG)

输出:

 资料来源:本地数据框架[6 x 3] 
组:GLB,NDG

基因GLB NDG
1 A4GNT 3 1
2 A4GNT 3 2
3 ABTB1 3 2
4 AADAC 10 1
5 AADAC 10 2
6 ABHD14B 10 2

但是,它出现以下错误:

  eval(expr,envir,enclosure)中的错误:未找到对象'NDG'

通过比较,当我使用代码



<$ p $时, dplyr :: slice 给出了正确的输出p> > dg%>%dplyr :: slice(1:unique(NDG))

在这种情况下,使用独特稍微黑客,但代码

 > dg%>%dplyr :: slice(1:NDG)

返回以下警告消息



 警告消息:
1:在slice_impl(.data,dots)中:
数字表达式有3个元素:只有第一个使用
2:在slice_impl(.data,dots)中:
数字表达式有3个元素:只有第一个使用
3:在slice_impl(.data,dots)中:
数字表达式有3个元素:只有第一个使用
4:在slice_impl(.data,dots)中:
数字表达式有3个元素:只有第一个使用

由于 NDG 正在评估(在适当的环境中)为 c(1,1,1) c(2,2,2),因此 1: NDG 返回上述警告。






关于为什么我收到错误,我知道代码Hadley用于方法sample_n.grouped_df是

  sample_n.grouped_df<  -  function(tbl,size,replace = FALSE,weight = NULL,
.env = parent.frame()){

assert_that(is.numeric(size),length(size)== 1,size> = 0 )
weight< - substitute(weight)

index< - attr(tbl,indices)
samples< - lapply(index,sample_group,frac = FALSE ,
tbl = tbl,size = size,replace = replace,weight = weight,.env = .env)
idx< - unlist(sampled)+ 1

grouping_df (tbl [idx,,drop = FALSE],vars = groups(tbl))
}

可以在相关的 Github页面中找到。因此,我得到错误,因为 sample_n.grouped_df 找不到变量 NGD ,因为它看起来不正确。 / p>

因此,在 dg sample_n 的整洁方法c>获取

 资料来源:本地数据框架[6 x 3] 
组:GLB,NDG

基因GLB NDG
1 A4GNT 3 1
2 A4GNT 3 2
3 ABTB1 3 2
4 AADAC 10 1
5 AADAC 10 2
6 ABHD14B 10 2

通过对每个组使用随机抽样?

解决方案

一个可能的答案,但我不相信这是最佳答案:使用 dplyr ::排列数据框的行sample_frac (和一小部分1),然后切片所需的行数:

 > set.seed(1)
> dg%>%
dplyr :: sample_frac(1)%>%
dplyr :: slice(1:unique(NDG))

这给出了正确的输出。

 来源:本地数据框[6 x 3] 
组:GLB,NDG

基因GLB NDG
1 A4GNT 3 1
2 AHSG 3 2
3 A4GNT 3 2
4 ACVR2B 10 1
5 AADAC 10 2
6 ACVR2B 10 2

我想我可以写一个函数,如果需要,可以一行一行。


I have the following grouped data frame, and I would like to use the function dplyr::sample_n to extract rows from this data frame for each group. I want to use the value of the grouped variable NDG in each group as the number of rows to extract from each group.

> dg.tmp <- structure(list(Gene = c("CAMK1", "GHRL", "TIMP4", "CAMK1", "GHRL", 
"TIMP4", "ARL8B", "ARPC4", "SEC13", "ARL8B", "ARPC4", "SEC13"
), GLB = c(3, 3, 3, 3, 3, 3, 10, 10, 10, 10, 10, 10), NDG = c(1, 
1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2)), class = c("tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -12L), .Names = c("Gene", "GLB", 
"NDG"))

> dg <- dg.tmp %>% 
     dplyr::group_by(GLB,NDG)

> dg
Source: local data frame [12 x 3]
Groups: GLB, NDG

      Gene GLB NDG
1    A4GNT   3   1
2    ABTB1   3   1
3     AHSG   3   1
4    A4GNT   3   2
5    ABTB1   3   2
6     AHSG   3   2
7    AADAC  10   1
8  ABHD14B  10   1
9   ACVR2B  10   1
10   AADAC  10   2
11 ABHD14B  10   2
12  ACVR2B  10   2

For example, assuming the correct random selection, I want the code

> dg %>% dplyr::sample_n(NDG)

to output:

Source: local data frame [6 x 3]
Groups: GLB, NDG

      Gene GLB NDG
1    A4GNT   3   1
2    A4GNT   3   2
3    ABTB1   3   2
4    AADAC  10   1
5    AADAC  10   2
6  ABHD14B  10   2

However, it gives the following error:

Error in eval(expr, envir, enclos) : object 'NDG' not found

By way of comparison, dplyr::slice gives the correct output when I use the code

> dg %>% dplyr::slice(1:unique(NDG))

It is slightly hackish using unique in this context, however, the code

> dg %>% dplyr::slice(1:NDG)

returns the following warning messages

Warning messages:
1: In slice_impl(.data, dots) :
  numerical expression has 3 elements: only the first used
2: In slice_impl(.data, dots) :
  numerical expression has 3 elements: only the first used
3: In slice_impl(.data, dots) :
  numerical expression has 3 elements: only the first used
4: In slice_impl(.data, dots) :
  numerical expression has 3 elements: only the first used

clearly because NDG is being evaluated (in the appropriate environment) as c(1,1,1) or c(2,2,2), and hence 1:NDG returns the above warning.


Regarding why I obtain the error, I know that the code Hadley uses for the method sample_n.grouped_df is

sample_n.grouped_df <- function(tbl, size, replace = FALSE, weight = NULL,
  .env = parent.frame()) {

  assert_that(is.numeric(size), length(size) == 1, size >= 0)
  weight <- substitute(weight)

  index <- attr(tbl, "indices")
  sampled <- lapply(index, sample_group, frac = FALSE,
    tbl = tbl, size = size, replace = replace, weight = weight, .env = .env)
  idx <- unlist(sampled) + 1

  grouped_df(tbl[idx, , drop = FALSE], vars = groups(tbl))
}

which can be found on the relevant Github page. Thus I obtain the error because sample_n.grouped_df cannot find the variable NGD because it's not looking in the correct environment.

Consequently, is there a neat way of using sample_n on dg to obtain

Source: local data frame [6 x 3]
Groups: GLB, NDG

      Gene GLB NDG
1    A4GNT   3   1
2    A4GNT   3   2
3    ABTB1   3   2
4    AADAC  10   1
5    AADAC  10   2
6  ABHD14B  10   2

by using random sampling on each group?

解决方案

One possible answer, but I'm not convinced it's the optimal answer: permute the rows of the data frame with dplyr::sample_frac (and a fraction of 1), then slice the required number of rows:

> set.seed(1)
> dg %>% 
      dplyr::sample_frac(1) %>%
      dplyr::slice(1:unique(NDG))

This gives the correct output.

Source: local data frame [6 x 3]
Groups: GLB, NDG

    Gene GLB NDG
1  A4GNT   3   1
2   AHSG   3   2
3  A4GNT   3   2
4 ACVR2B  10   1
5  AADAC  10   2
6 ACVR2B  10   2

And I suppose I can just write a function to do this in one line if necessary.

这篇关于dplyr sample_n其中n是分组变量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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