Learn R Programming

collapse (version 1.1.0)

AA2-small-helpers: collapse Small (Helper) Functions

Description

Elementary exported convenience functions in the collapse package, that help to deal with variable names, labels, missing values, matching and object checking etc... For recoding and replacing values see Recode.

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, attrn = "label") # Return data.frame of names, labels and classes
add_stub(X, stub, pre = TRUE) # Add a stub (i.e. prefix or postfix) to column names
rm_stub(X, stub, ...)         # Remove stub from column names (using base::sub)
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
finteraction(...)             # Faster interactions, unused levels are dropped
na_rm(x)                      # Remove missing values from vector and return vector
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)                    # Integer sequences along rows of X
seq_col(X)                    # 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
is.categorical(x)             # The opposite of is.numeric
is.Date(x)                    # Check if object is of class "Date", "POSIXlt" or "POSIXct"
as.numeric_factor(X)          # Coerce a factor, or all factor columns in X, to numeric
as.character_factor(X)        # Coerce a factor, or all factor columns in X, to character

Arguments

X

a matrix or data.frame.

object

a matrix, array or data.frame.

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.

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 all_identical / all_obj_equal / finteraction: either multiple comma-separated objects ( = vectors or factors in the case of finteraction) or a single list of objects. For rm_stub: Other arguments passed to sub.

See Also

Recode, 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.")                # use "^log." or ".log$" to only match beginning or end

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

## Factor to numeric conversions:
identical(mtcars,  as.numeric_factor(dapply(mtcars, qF)))

## Fast interactions
finteraction(mtcars$cyl, mtcars$vs)
finteraction(mtcars)
# }

Run the code above in your browser using DataLab