R/split.r

Defines functions print.split as.list.split

Documented in as.list.split print.split

#' Subset splits.
#'
#' Subset splits, ensuring that labels keep matching
#'
#' @keywords internal
#' @param x split object
#' @param i index
#' @param ... unused
#' @method [ split
#' @rdname get-split
#' @export
"[.split" <- function(x, i, ...) {
  structure(
    NextMethod(),
    class = c("split", "list"),
    split_type = attr(x, "split_type"),
    split_labels = attr(x, "split_labels")[i, , drop = FALSE]
  )
}

#' Convert split list to regular list.
#'
#' Strip off label related attributed to make a strip list as regular list
#'
#' @keywords internal
#' @param x object to convert to a list
#' @param ... unused
#' @method as.list split
#' @export
as.list.split <- function(x, ...) {
  attr(x, "split_type") <- NULL
  attr(x, "split_labels") <- NULL
  class(x) <- setdiff(class(x), "split")
  x
}

#' Print split.
#'
#' Don't print labels, so it appears like a regular list
#'
#' @keywords internal
#' @param x object to print
#' @param ... unused
#' @method print split
#' @export
print.split <- function(x, ...) {
  print(as.list(x))
}

Try the plyr package in your browser

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

plyr documentation built on Oct. 2, 2023, 9:07 a.m.