Convert common data objects quickly, without method dispatch and extensive checks:
qDF
and qDT
convert vectors, matrices, higher-dimensional arrays and suitable lists to data.frame
and data.table respectively.
qM
converts vectors, higher-dimensional arrays, data.frames and suitable lists to matrix.
mctl
and mrtl
column- or row-wise convert a matrix to list, data.frame
or data.table. They are used internally by qDF
and qDT
, dapply
, BY
, etc...
qF
converts atomic vectors to factor (documented on a separate page).
as.numeric_factor
and as.character_factor
convert factors, or all factor columns in a list, to numeric or character (by converting the levels).
qDF(X, row.names.col = FALSE)
qDT(X, row.names.col = FALSE)
qM(X)
mctl(X, names = FALSE, return = "list")
mrtl(X, names = FALSE, return = "list")
as.numeric_factor(X, keep.attr = TRUE)
as.character_factor(X, keep.attr = TRUE)
a vector, factor, matrix, higher-dimensional array, data.frame or list. mctl
and mrtl
only take matrices.
should a column capturing names or row.names be added? i.e. when converting atomic objects to data.frame or data.frame to data.table. Can be logical TRUE
, which will add a column "row.names"
in front, or can supply a name for the column i.e. "column1"
.
logical. Should the list be named?
an integer or string specifying what to return. The options are:
Int. | String | Description | ||
1 | "list" | returns a plain list | ||
2 | "data.frame" | returns a data.frame | ||
3 | "data.table" | returns a data.table |
logical. TRUE
keeps all attributes of factor variables apart from the levels and class attributes (such as variable labels etc.).
qDF
- returns a data.frame
qDT
- returns a data.table
qM
- returns a matrix
mctl
, mrtl
- return a list, data.frame or data.table
qF
- returns a factor
as.numeric_factor
- returns X with factors converted to numeric variables
as.character_factor
- returns X with factors converted to character variables
# NOT RUN {
mtcarsM <- qM(mtcars) # Matrix from data.frame
mtcarsDT <- qDT(mtcarsM) # data.table from matrix columns
mrtl(mtcarsM, TRUE, "data.frame") # data.frame from matrix rows, etc...
qDF(mtcarsM, "cars") # Adding a row.names column when converting from matrix
qDT(mtcars, "cars") # Saving row.names when converting data.frame to data.table
cylF <- qF(mtcars$cyl) # Factor from atomic vector
cylF
## Factor to numeric conversions
identical(mtcars, as.numeric_factor(dapply(mtcars, qF)))
# }
Run the code above in your browser using DataLab