
collapse
Small (Helper) FunctionsElementary 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
.
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%
fnlevels(x) # Faster version of nlevels(x) (for factors)
funique(x, ordered = TRUE) # Faster unique(x) and sort(unique(x)) for vectors
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
a matrix or data.frame.
a matrix, array or data.frame.
a atomic vector.
character. Name of attribute to store labels or retrieve labels from.
a matching character vector of variable labels.
logical. Also show the classes of variables in X in a column?
a single character stub, i.e. "log.", which by default will be pre-applied to all variables or column names in X.
logical. FALSE
will post-apply stub
.
a suitable vector of row- or column-names.
a suitable list of dimension names.
logical. TRUE
(default) sorts the output, FALSE
is slightly faster.
specify the proportion of observations randomly replaced with NA
.
for all_identical / all_obj_equal
: either multiple comma-separated objects or a single list of objects. For rm_stub
: Other arguments passed to sub
.
# 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)))
# }
Run the code above in your browser using DataLab