Learn R Programming

doBy (version 4.6.26)

by-split: Split a data frame

Description

Split a dataframe according to the levels of variables in the dataframe. The variables to split by can be given as a formula or as a character vector.

Usage

split_by(data, ..., omit = TRUE)

splitBy(formula, data, omit = TRUE)

# S3 method for splitByData head(x, n = 6L, ...)

# S3 method for splitByData tail(x, n = 6L, ...)

split_by.legacy(data, formula, drop = TRUE)

splitBy.legacy(formula, data = parent.frame(), drop = TRUE)

Value

A list of dataframes.

Arguments

data

A data frame

...

A character vector or, right hand sided formula or a listing of variables.

omit

Logical Should variables split on be omitted from the output. Defaults to TRUE.

formula

A character vector or a right hand sided formula.

x

An object.

n

A single integer. If positive or zero, size for the resulting object: number of elements for a vector (including lists), rows for a matrix or data frame or lines for a function. If negative, all but the "n" last/first number of elements of "x".

drop

Obsolete

Author

Søren Højsgaard, sorenh@math.aau.dk

See Also

orderBy, order_by, summaryBy, summary_by, transformBy, transform_by

Examples

Run this code

split_by(CO2, ~Treatment+Type)
split_by(CO2, Treatment, Type)
split_by(CO2, c("Treatment", "Type"))
split_by(CO2, Treatment)
x <- CO2 |> split_by(~Treatment)
head(x, 3)
tail(x, 3)

x <- CO2 |> split_by(~Treatment, omit=FALSE)
head(x, 3)
tail(x, 3)

## The "Old" interface
splitBy(~Treatment+Type, CO2)
splitBy(~Treatment+Type, data=CO2)
splitBy(c("Treatment", "Type"), data=CO2)

Run the code above in your browser using DataLab