small-helpers function

Small (Helper) Functions

Small (Helper) Functions

Convenience functions in the collapse package that help to deal with object attributes such as variable names and labels, object checking, metaprogramming, and that improve the workflow.

.c(...) # Non-standard concatenation i.e. .c(a, b) == c("a", "b") nam %=% values # Multiple-assignment e.g. .c(x, y) %=% c(1, 2), massign(nam, values, # can also assign to different environment. envir = parent.frame()) vlabels(X, attrn = "label", # Get labels of variables in X, in attr(X[[i]], attrn) use.names = TRUE) vlabels(X, attrn = "label") <- value # Set labels of variables in X (by reference) setLabels(X, value = NULL, # Set labels of variables in X (by reference) and return X attrn = "label", cols = NULL) vclasses(X, use.names = TRUE) # Get classes of variables in X namlab(X, class = FALSE, # Return data frame of names and labels, attrn = "label", N = FALSE, # and (optionally) classes, number of observations Ndistinct = FALSE) # and number of non-missing distinct values add_stub(X, stub, pre = TRUE, # Add a stub (i.e. prefix or postfix) to column names cols = NULL) rm_stub(X, stub, pre = TRUE, # Remove stub from column names, also supports general regex = FALSE, # regex matching and removing of characters cols = NULL, ...) all_identical(...) # Check exact equality of multiple objects or list-elements all_obj_equal(...) # Check near equality of multiple objects or list-elements all_funs(expr) # Find all functions called in an R language expression setRownames(object, # Set rownames of object and return object nm = if(is.atomic(object)) seq_row(object) else NULL) setColnames(object, nm) # Set colnames of object and return object setDimnames(object, dn, # Set dimension names of object and return object which = NULL) unattrib(object) # Remove all attributes from object setAttrib(object, a) # Replace all attributes with list of attributes 'a' setattrib(object, a) # Same thing by reference, returning object invisibly copyAttrib(to, from) # Copy all attributes from object 'from' to object 'to' copyMostAttrib(to, from) # Copy most attributes from object 'from' to object 'to' 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).
  • x: a (atomic) vector.
  • expr: an expression of type "language" e.g. quote(x / sum(x)).
  • object, to, from: a suitable R object.
  • a: a suitable list of attributes.
  • attrn: character. Name of attribute to store labels or retrieve labels from.
  • N, Ndistinct: logical. Options to display the number of observations or number of distinct non-missing values.
  • value: for whichv and alloc: a single value of any vector type. For vlabels<- and setLabels: a matching character vector or list of variable labels.
  • use.names: logical. Preserve names if X is a list.
  • cols: integer. (optional) indices of columns to apply the operation to. Note that for these small functions this needs to be integer, whereas for other functions in the package this argument is more flexible.
  • 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.
  • regex: logical. Match pattern anywhere in names using a regular expression and remove it with gsub.
  • nm: a suitable vector of row- or column-names.
  • dn: a suitable vector or list of names for dimension(s).
  • which: integer. If NULL, dn has to be a list fully specifying the dimension names of the object. Alternatively, a vector or list of names for dimensions which can be supplied. See Examples.
  • nam: character. A vector of object names.
  • values: a matching atomic vector or list of objects.
  • envir: the environment to assign into.
  • ...: for .c: Comma-separated expressions. 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 / numeric equality. For rm_stub: further arguments passed to gsub.

Details

all_funs is the opposite of all.vars, to return the functions called rather than the variables in an expression. See Examples.

copyAttrib and copyMostAttrib take a shallow copy of the attribute list, i.e. they don't duplicate in memory the attributes themselves. They also, along with setAttrib, take a shallow copy of lists passed to the to argument, so that lists are not modified by reference. Atomic to arguments are however modified by reference. The function setattrib, added in v1.8.9, modifies the object by reference i.e. no shallow copies are taken.

copyMostAttrib copies all attributes except for "names", "dim" and "dimnames" (like the corresponding C-API function), and further only copies the "row.names" attribute of data frames if known to be valid. Thus it is a suitable choice if objects should be of the same type but are not of equal dimensions.

See Also

Efficient Programming , Collapse Overview

Examples

## Non-standard concatenation .c(a, b, "c d", e == f) ## Multiple assignment .c(a, b) %=% list(1, 2) .c(T, N) %=% dim(EuStockMarkets) names(iris) %=% iris list2env(iris) # Same thing rm(list = c("a", "b", "T", "N", names(iris))) ## Variable labels namlab(wlddev) namlab(wlddev, class = TRUE, N = TRUE, Ndistinct = TRUE) vlabels(wlddev) vlabels(wlddev) <- vlabels(wlddev) ## Stub-renaming log_mtc <- add_stub(log(mtcars), "log.") head(log_mtc) head(rm_stub(log_mtc, "log.")) rm(log_mtc) ## Setting dimension names of an object head(setRownames(mtcars)) ar <- array(1:9, c(3,3,3)) setRownames(ar) setColnames(ar, c("a","b","c")) setDimnames(ar, c("a","b","c"), which = 3) setDimnames(ar, list(c("d","e","f"), c("a","b","c")), which = 2:3) setDimnames(ar, list(c("g","h","i"), c("d","e","f"), c("a","b","c"))) ## 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) ## Function names from expressions ex = quote(sum(x) + mean(y) / z) all.names(ex) all.vars(ex) all_funs(ex) rm(ex)
  • Maintainer: Sebastian Krantz
  • License: GPL (>= 2) | file LICENSE
  • Last published: 2025-03-10