R/sample_n.R

Defines functions sample_n.morphed_tbl_graph sample_n.tbl_graph

#' @export
#' @importFrom dplyr sample_n
#' @importFrom igraph delete_vertices delete_edges
#' @importFrom rlang enquo
sample_n.tbl_graph <- function(tbl, size = 1, replace = FALSE, weight = NULL, .env = parent.frame(), ...) {
  tbl <- unfocus(tbl)
  d_tmp <- as_tibble(tbl)
  weight <- enquo(weight)
  check_reserved(d_tmp)
  orig_ind <- seq_len(nrow(d_tmp))
  d_tmp$.tbl_graph_index <- orig_ind
  d_tmp <- sample_n(d_tmp, size = size, replace = replace, weight = !! weight, .env = .env)
  remove_ind <- orig_ind[-d_tmp$.tbl_graph_index]
  switch(
    active(tbl),
    nodes = delete_vertices(tbl, remove_ind),
    edges = delete_edges(tbl, remove_ind)
  ) %gr_attr% tbl
}
#' @export
#' @importFrom dplyr sample_frac
#' @importFrom rlang enquo
sample_n.morphed_tbl_graph <- function(tbl, size = 1, replace = FALSE, weight = NULL, .env = parent.frame(), ...) {
  weight <- enquo(weight)
  tbl[] <- lapply(tbl, sample_n, size = size, replace = replace, weight = !! weight, .env = .env)
  tbl
}

#' @export
dplyr::sample_n

Try the tidygraph package in your browser

Any scripts or data that you put into this service are public.

tidygraph documentation built on June 22, 2024, 11:32 a.m.