Learn R Programming

collapse (version 1.1.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 are exported C++ functions that 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.

  • qG is a programmers function that converts atomic vectors to a quick-group (class attribute 'qG') - a kind of factor light which is just an integer vector with an attribute 'N.groups' indicating the number of groups. (Thus saving time consumed by converting the levels of a numeric vector to character).

Usage

qDF(X, row.names.col = FALSE)
qDT(X, row.names.col = FALSE)
qM(X)
mctl(X, names = FALSE, ret = 0L)
mrtl(X, names = FALSE, ret = 0L)
qF(x, ordered = TRUE, na.exclude = TRUE)
qG(x, ordered = TRUE, na.exclude = TRUE)

Arguments

X

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

x

a atomic vector or factor.

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

ordered

logical. TRUE sorts the levels and returns an ordered factor.

names

logical. Should the list be named?

ret

integer. 3 return options: 0L - return a list, 1L - return a data.frame, 2L - return a data.table.

na.exclude

logical. TRUE preserves missing values in character or numeric vectors (i.e. no underlying integer is generated to represent NA, and functions like na_rm will remove missing values from the factor). FALSE creates an integer for NA and also attaches a class 'na.included', which will make computations with collapse functions faster (since no NA checking is required). Note that in both cases a 'NA' level is attached, so this option only concerns the underlying integer vector and the response to functions dealing with missing values (like na_rm).

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 qG - returns a quick-group (= integer vector)

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, 2L)    # 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
cylG <- qG(mtcars$cyl)     # Quick-group from atomic vector
cylG                       # See the simple structure of this object
# }

Run the code above in your browser using DataLab