R/sample.R

Defines functions sample_frac.grouped_dt sample_frac.tbl_dt sample_n.grouped_dt sample_n.tbl_dt

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

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

  idx_call <- substitute(
    list(`row_` = .I[sample(.N, size = size, replace = replace, prob = weight)]),
    list(size = size, replace = replace, weight = substitute(weight))
  )
  idx <- dt_subset(tbl, , idx_call, env = .env)$row_

  grouped_dt(tbl[idx], groups(tbl))
}

sample_frac.tbl_dt <- function(tbl, size = 1, replace = FALSE, weight = NULL,
                               .env = parent.frame()) {
  tbl_dt(NextMethod())
}

sample_frac.grouped_dt <- function(tbl, size = 1, replace = FALSE, weight = NULL,
                                   .env = parent.frame()) {

  idx_call <- substitute(
    list(`row_` = .I[sample(.N, size = round(size * .N), replace = replace, prob = weight)]),
    list(size = size, replace = replace, weight = substitute(weight))
  )
  idx <- dt_subset(tbl, , idx_call, env = .env)$row_

  grouped_dt(tbl[idx], groups(tbl))
}
hadley/dtplyr documentation built on June 5, 2017, 12:01 p.m.