Learn R Programming

collapse (version 1.2.0)

A4-quick-conversion: Quick Data Conversion

Description

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

Usage

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

Arguments

X

a vector, matrix, higher-dimensional array, data.frame or list. mctl and mrtl only take matrices.

row.names.col

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

names

logical. Should the list be named?

return

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

Value

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

See Also

GRP, Collapse Overview

Examples

Run this code
# NOT RUN {
mtcarsM <- qM(mtcars)      # Matrix from data.frame
mtcarsDT <- qDT(mtcarsM)   # data.table from matrix columns
mrtl(mtcarsM, TRUE, 3L)    # data.table 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
# }

Run the code above in your browser using DataLab