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.

split_by(data, formula, drop = TRUE)

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

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

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

Arguments

data

A data frame

formula

Variables to split data frame by, as as.quoted variables, a formula or character vector.

drop

Logical indicating if levels that do not occur should be dropped. Deprecated; levels that do not occur are ignored.

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".

...

Arguments to be passed to or from other methods.

Value

A list of dataframes.

Author

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

Examples


data(dietox, package="doBy")
splitBy(formula = ~Evit + Cu, data = dietox)
#>       listentry    Evit    Cu
#> 1 Evit000|Cu000 Evit000 Cu000
#> 2 Evit000|Cu035 Evit000 Cu035
#> 3 Evit200|Cu175 Evit200 Cu175
#> 4 Evit100|Cu035 Evit100 Cu035
#> 5 Evit100|Cu175 Evit100 Cu175
#> 6 Evit200|Cu035 Evit200 Cu035
#> 7 Evit200|Cu000 Evit200 Cu000
#> 8 Evit000|Cu175 Evit000 Cu175
#> 9 Evit100|Cu000 Evit100 Cu000
splitBy(formula = c("Evit", "Cu"), data = dietox)
#>       listentry    Evit    Cu
#> 1 Evit000|Cu000 Evit000 Cu000
#> 2 Evit000|Cu035 Evit000 Cu035
#> 3 Evit200|Cu175 Evit200 Cu175
#> 4 Evit100|Cu035 Evit100 Cu035
#> 5 Evit100|Cu175 Evit100 Cu175
#> 6 Evit200|Cu035 Evit200 Cu035
#> 7 Evit200|Cu000 Evit200 Cu000
#> 8 Evit000|Cu175 Evit000 Cu175
#> 9 Evit100|Cu000 Evit100 Cu000

splitBy(~Treatment + Type, data=CO2)
#>                listentry  Treatment        Type
#> 1      nonchilled|Quebec nonchilled      Quebec
#> 2         chilled|Quebec    chilled      Quebec
#> 3 nonchilled|Mississippi nonchilled Mississippi
#> 4    chilled|Mississippi    chilled Mississippi
splitBy(c("Treatment", "Type"), data=CO2)
#>                listentry  Treatment        Type
#> 1      nonchilled|Quebec nonchilled      Quebec
#> 2         chilled|Quebec    chilled      Quebec
#> 3 nonchilled|Mississippi nonchilled Mississippi
#> 4    chilled|Mississippi    chilled Mississippi

x <- splitBy(~Treatment, data=CO2)
head(x)
#> $nonchilled
#>   Plant   Type  Treatment conc uptake
#> 1   Qn1 Quebec nonchilled   95   16.0
#> 2   Qn1 Quebec nonchilled  175   30.4
#> 3   Qn1 Quebec nonchilled  250   34.8
#> 4   Qn1 Quebec nonchilled  350   37.2
#> 5   Qn1 Quebec nonchilled  500   35.3
#> 6   Qn1 Quebec nonchilled  675   39.2
#> 
#> $chilled
#>    Plant   Type Treatment conc uptake
#> 22   Qc1 Quebec   chilled   95   14.2
#> 23   Qc1 Quebec   chilled  175   24.1
#> 24   Qc1 Quebec   chilled  250   30.3
#> 25   Qc1 Quebec   chilled  350   34.6
#> 26   Qc1 Quebec   chilled  500   32.5
#> 27   Qc1 Quebec   chilled  675   35.4
#> 
tail(x)
#> $nonchilled
#>    Plant        Type  Treatment conc uptake
#> 58   Mn3 Mississippi nonchilled  175   19.4
#> 59   Mn3 Mississippi nonchilled  250   25.8
#> 60   Mn3 Mississippi nonchilled  350   27.9
#> 61   Mn3 Mississippi nonchilled  500   28.5
#> 62   Mn3 Mississippi nonchilled  675   28.1
#> 63   Mn3 Mississippi nonchilled 1000   27.8
#> 
#> $chilled
#>    Plant        Type Treatment conc uptake
#> 79   Mc3 Mississippi   chilled  175   18.0
#> 80   Mc3 Mississippi   chilled  250   17.9
#> 81   Mc3 Mississippi   chilled  350   17.9
#> 82   Mc3 Mississippi   chilled  500   17.9
#> 83   Mc3 Mississippi   chilled  675   18.9
#> 84   Mc3 Mississippi   chilled 1000   19.9
#>