Last data update: 2014.03.03

R: Combine Levels of Different Factors to Groups
factorCombinationsR Documentation

Combine Levels of Different Factors to Groups

Description

All levels of the factors from a data.frame provided are combined, each combination of factor levels is referred to as group.

Usage

factorCombinations(X, factor.sep = "|", level.sep = ".", count=TRUE)

Arguments

X

A data.frame containing at least one factor.

factor.sep

Character by which the factor-factor level combinations are separated in the group names.

level.sep

Character by which the level names are separated from the corresponding factor names in the group names.

count

Should the occurences of the different groups in the data.frame provided be counted? Default to TRUE.

Value

A list with the following components:

combinations

A data frame containing every combination of factor levels (groups) for the factors provided.

names

A character vector with the names of the groups. Factor-factor level combinations are separated by factor.sep and level names from the corresponding factor names by level.sep.

counts

If count is TRUE a vector indicating the number of occurences of the different groups within the data.frame provided to the function. NULL if count is set to FALSE.

Note

Function is mainly for internal use in LinRegInteractive but may be useful for other purposes as well.

Examples

# multiple factors
data("CO2")
print(factorCombinations(CO2))

# single factor
data("chickwts")
print(factorCombinations(chickwts))

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(LinRegInteractive)
Error in library(LinRegInteractive) : 
  there is no package called 'LinRegInteractive'
Execution halted