Rev 80480 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
# File src/library/base/R/split.R# Part of the R package, https://www.R-project.org## Copyright (C) 1995-2018 The R Core Team## This program is free software; you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation; either version 2 of the License, or# (at your option) any later version.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.## A copy of the GNU General Public License is available at# https://www.R-project.org/Licenses/split <- function(x, f, drop = FALSE, ...) UseMethod("split")split.default <- function(x, f, drop = FALSE, sep = ".", lex.order = FALSE, ...){if(!missing(...)) .NotYetUsed(deparse(...), error = FALSE)if (is.list(f))f <- interaction(f, drop = drop, sep = sep, lex.order = lex.order)else if (!is.factor(f)) f <- as.factor(f) # docs say as.factorelse if (drop) f <- factor(f) # drop extraneous levelsstorage.mode(f) <- "integer" # some factors have had double in the pastif (is.null(attr(x, "class")))return(.Internal(split(x, f)))## elseind <- .Internal(split(seq_along(x), f))lapply(ind, function(i) x[i])}## This is documented to work for matrices toosplit.data.frame <- function(x, f, drop = FALSE, ...){## If formula, maybe should check that there is no LHS?if (inherits(f, "formula"))f <- eval(attr(stats::terms(f), "variables"), x, environment(f))lapply(split(x = seq_len(nrow(x)), f = f, drop = drop, ...),function(ind) x[ind, , drop = FALSE])}`split<-` <- function(x, f, drop = FALSE, ..., value) UseMethod("split<-")`split<-.default` <- function(x, f, drop = FALSE, ..., value){ix <- split(seq_along(x), f, drop = drop, ...)n <- length(value)j <- 0for (i in ix) {j <- j %% n + 1x[i] <- value[[j]]}x}## This is documented to work for matrices too`split<-.data.frame` <- function(x, f, drop = FALSE, ..., value){if (inherits(f, "formula")) f <- eval(attr(stats::terms(f), "variables"), x, environment(f))ix <- split(seq_len(nrow(x)), f, drop = drop, ...)n <- length(value)j <- 0for (i in ix) {j <- j %% n + 1x[i,] <- value[[j]]}x}## (Note: use rep(NA_integer_, len) for indexing here. Logical NA confuses## tibbles and only coincidentally does the right thing otherwise,## because len is longer than value[[1]]. Otherwise recycling would kick in.)unsplit <- function (value, f, drop = FALSE){len <- length(if (is.list(f)) f[[1L]] else f)if (is.data.frame(value[[1L]])) {x <- value[[1L]][rep(NA_integer_, len),, drop = FALSE]rownames(x) <- unsplit(lapply(value, rownames), f, drop = drop)} elsex <- value[[1L]][rep(NA_integer_, len)]split(x, f, drop = drop) <- valuex}