简体   繁体   English

R- data.table中的组的随机样本

[英]R- random sample of groups in a data.table

How can I randomly sample eg three groups within a data.table so that the result contains three groups with all rows from the original data.table? 如何在data.table中随机采样例如三组,以使结果包含三组,其中原始data.table中的所有行均如此?

library(data.table)
dat <- data.table(ids=1:20, groups=sample(x=c("A","B","C", "D", "E", "F"), 20, 
replace=TRUE))

I know how to select 10 rows randomly from a data.table: 我知道如何从data.table中随机选择10行:

dat.sampl1 <- as.data.table(sapply(dat[], sample, 10))

And also how to sample by group 还有如何按组抽样

dat[,.SD[sample(.N, min(.N,3))], by = groups]

But how to randomly sample groups? 但是如何随机分组? So the result should look like: 因此结果应如下所示:

    ids groups
     1      F
    11      F
     3      F
    18      F
     8      A
     9      A
    10      A
    17      A
    19      A
    12      E
    14      E
    16      E

Do you mean something like: 您的意思是:

set.seed(123)
dat <- data.table(ids=1:20, groups=sample(x=c("A","B","C", "D", "E", "F"), 20, 
replace=TRUE))
dat[groups %in% sample(unique(dat[, groups]), size = 3)][order(groups)]
#     ids groups
#  1:   3      C
#  2:  10      C
#  3:  12      C
#  4:   7      D
#  5:   9      D
#  6:  14      D
#  7:   4      F
#  8:   5      F
#  9:   8      F
# 10:  11      F
# 11:  16      F
# 12:  20      F

If you want to sample groups with replacement , you can do the following, where A has been sampled twice: 如果要对具有replacement的组进行抽样,则可以执行以下操作,其中对A进行了两次抽样:

dat[unique(dat[, list(groups)])[sample(.N, 3, replace = TRUE)], on = "groups"]
#    ids groups
# 1:   3      C
# 2:  10      C
# 3:  12      C
# 4:   6      A
# 5:  15      A
# 6:  18      A
# 7:   6      A
# 8:  15      A
# 9:  18      A

This code works, using a single line of base R code using %in% to check an index which is generated using the sample function: 此代码有效,使用单行基础R代码(使用%in%检查使用sample函数生成的索引:

df1[df1[,'groups'] %in% sample(unique(df1[,'groups']), size = 3, replace = F), ]

For example: 例如:

> df1 <- data.frame("ids" = 1:20, "groups" = sample(LETTERS[1:4], size = 20, replace = T))
> df2 <- df1[df1[,'groups'] %in% sample(unique(df1[,'groups']), size = 3, replace = F), ]
> df2[order(df2[,'groups']),]
   ids groups
4    4      B
6    6      B
18  18      B
20  20      B
1    1      C
2    2      C
3    3      C
9    9      C
12  12      C
16  16      C
19  19      C
7    7      D
11  11      D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM