DataFrameFactor-class {S4Vectors}R Documentation

DataFrameFactor objects

Description

The DataFrameFactor class is a subclass of the Factor class where the levels are the rows of a DataFrame. It provides a few methods to mimic the behavior of an actual DataFrame while retaining the memory efficiency of the Factor structure.

Usage

DataFrameFactor(x, levels, index=NULL, ...)  # constructor function

Arguments

x, levels

DataFrame objects. At least one of x and levels must be specified. If index is NULL, both can be specified.

When levels is specified, it must be a DataFrame with no duplicate rows (i.e. anyDuplicated(levels) must return 0).

See ?Factor for more details.

index

NULL or an integer (or numeric) vector of valid positive indices (no NAs) into levels. See ?Factor for details.

...

Optional metadata columns.

Value

A DataFrameFactor object.

Accessors

DataFrameFactor objects support the same set of accessors as Factor objects. In addition, it mimics some aspects of the DataFrame interface. The general principle is that, for these methods, a DataFrameFactor x behaves like the expanded DataFrame unfactor(x).

Caution

The DataFrame-like methods implemented here are for convenience only. Users should not assume that the DataFrameFactor complies with other aspects of the DataFrame interface, due to fundamental differences between a DataFrame and the Factor parent class, e.g., in the interpretation of their “length”. Outside of the methods listed above, the DataFrameFactor is not guaranteed to work as a drop-in replacement for a DataFrame - use unfactor(x) instead.

Author(s)

Aaron Lun

See Also

Factor objects for the parent class.

Examples

df <- DataFrame(X=sample(5, 100, replace=TRUE), Y=sample(c("A", "B"), 100, replace=TRUE))
dffac <- DataFrameFactor(df)
dffac

dffac$X
dffac[,c("Y", "X")]
dffac[1:10,"X"]
colnames(dffac)

# The usual Factor methods may also be used:
unfactor(dffac)
levels(dffac)
as.integer(dffac)

[Package S4Vectors version 0.40.2 Index]