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 When See |
index |
|
... |
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)
.
-
x$name
will return columnname
fromlevels(x)
and expand it according to the indices inx
. -
x[i, j, ..., drop=TRUE]
will return a new DataFrameFactor subsetted to entriesi
, where the levels are subsetted by column to contain only columnsj
. If the resulting levels only have one column anddrop=TRUE
, the expanded values of the column are returned directly. -
dim(x)
will return the length of the DataFrameFactor and the number of columns in its levels. -
dimnames(x)
will return the names of the DataFrameFactor and the column names in its levels.
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)