Validate selected elements from a character vector

Description

Validate selected elements from a character vector using a variety of selection mechanisms: logical, names, or numerical indexes

Usage

1
selectElements(elements, cVec)

Arguments

elements

elements to select from cVec. Can be a logical vector, a vector of numeric indexes, or a character vector of element names. Note that logical vectors are not recycled as usual: they must be the same length as cVec. If elements == NULL, NULL is returned.

cVec

A character vector from which to select elements (such as row names or column names)

Details

This function is especially useful for selecting rows or columns from data frames, while providing informative error messages if the elements for selection are specified incorrectly.

Value

A character vector with elements that were selected from cVec using elements

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# Define some "column names"
cnames <- letters[1:5]
cnames

# Select the 1st and 3rd column names using a variety of approaches
selectElements(c("a", "c"), cnames)
selectElements(c(1, 3), cnames)
selectElements(c(TRUE, FALSE, TRUE, FALSE, FALSE), cnames)

# Select the 1st, 3rd, and 1st columns
selectElements(c("a", "c", "a"), cnames)
selectElements(c(1, 3, 1), cnames)

# If you don't want to select any of them
selectElements(NULL, cnames)

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.