Learn R Programming

collapse (version 1.2.1)

AA2-small-helpers: Small (Helper) Functions

Description

Convenience functions in the collapse package that help to deal with variable names, labels, missing values, matching and object checking etc.. Some functions are performance improved replacements for base R functions.

Usage

vlabels(X, attrn = "label")   # Get labels of variables in X, in attr(X[[i]], attrn)
vlabels(X, attrn = "label") <- value      # Set labels of variables in X
vclasses(X)                   # Get classes of variables in X
vtypes(X)                     # Get data storage types of variables in X (calling typeof)
namlab(X, class = FALSE,      # Return data.frame of names, labels and classes
          attrn = "label")
add_stub(X, stub, pre = TRUE) # Add a stub (i.e. prefix or postfix) to column names
rm_stub(X, stub, pre = TRUE)  # Remove stub from column names
x %!in% table                 # The opposite of %in%
ckmatch(x, table,             # Check-match: throws an informative error if non-matched
     e = "Unknown columns:")
fnlevels(x)                   # Faster version of nlevels(x) (for factors)
funique(x, ordered = TRUE)    # Faster unique(x) and sort(unique(x)) for vectors
fnrow(X)                      # Faster nrow for data.frames (not faster for matrices)
fncol(X)                      # Faster ncol for data.frames (not faster for matrices)
fdim(X)                       # Faster dim for data.frames (not faster for matrices)
na_rm(x)                      # Remove missing values from vector and return vector
na_omit(X, cols = NULL,       # Faster na.omit for matrices and data.frames
        na.attr = FALSE)
na_insert(X, prop = 0.1)      # Insert missing values at random in vectors, matrices DF's
all_identical(...)            # Check exact equality of multiple objects or list-elements
all_obj_equal(...)            # Check near equality of multiple objects or list-elements
seq_row(X)                    # Fast integer sequences along rows of X
seq_col(X)                    # Fast integer sequences along columns of X
setRownames(object = nm,      # Set rownames of object and return object
        nm = seq_row(object))
setColnames(object = nm, nm)  # Set colnames of object and return object
setDimnames(object = dn, dn)  # Set dimension names of object and return object
unattrib(object)              # Remove all attributes from object
is.categorical(x)             # The opposite of is.numeric
is.Date(x)                    # Check if object is of class "Date", "POSIXlt" or "POSIXct"

Arguments

X

a matrix or data.frame (some functions also support vectors and arrays although that is less common).

object

a suitable R object.

x, table

a atomic vector.

attrn

character. Name of attribute to store labels or retrieve labels from.

value

a matching character vector of variable labels.

class

logical. Also show the classes of variables in X in a column?

stub

a single character stub, i.e. "log.", which by default will be pre-applied to all variables or column names in X.

pre

logical. FALSE will post-apply stub.

cols

only removes rows with missing values on these columns. Columns can be selected using column names, indices or a selector function (i.e. is.numeric).

na.attr

logical. TRUE adds an attribute containing the removed cases. For compatibility reasons this is exactly the same format as na.omit i.e. the attribute is called "na.action" and of class "omit".

nm

a suitable vector of row- or column-names.

dn

a suitable list of dimension names.

ordered

logical. TRUE (default) sorts the output, FALSE is slightly faster.

prop

specify the proportion of observations randomly replaced with NA.

e

The error message thrown by ckmatch for non-matched elements.

...

for all_identical / all_obj_equal: either multiple comma-separated objects or a single list of objects in which all elements will be checked for exact or numeric equality by all_identical and all_obj_equal, respectively.

See Also

Collapse Overview

Examples

Run this code
# NOT RUN {
## Variable labels
namlab(wlddev, class = TRUE)
vlabels(wlddev)
vlabels(wlddev) <- vlabels(wlddev)

## Stub-renaming
log_mtc <- add_stub(log(mtcars), "log.")
rm_stub(log_mtc, "log.")
rm(log_mtc)

## Checking exact equality of multiple objects
all_identical(iris, iris, iris, iris)
l <- replicate(100, fmean(num_vars(iris), iris$Species), simplify = FALSE)
all_identical(l)
rm(l)

## Missing values
mtc_na <- na_insert(mtcars, 0.15)    # Set 15% of values missing at random
fNobs(mtc_na)                        # See observation count
na_omit(mtc_na)                      # 12x faster than na.omit(airquality)
na_omit(mtc_na, na.attr = TRUE)      # Adds attribute with removed cases, like na.omit
na_omit(mtc_na, cols = c("vs","am")) # Removes only cases missing vs or am
na_omit(qM(mtc_na))                  # Also works for matrices
na_omit(mtc_na$vs, na.attr = TRUE)   # Also works with vectors
na_rm(mtc_na$vs)                     # For vectors na_rm is faster ...
rm(mtc_na)
# }

Run the code above in your browser using DataLab