Learn R Programming

Smisc (version 0.3.9.1)

selectElements: 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

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)

Value

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

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.

Examples

Run this code
# NOT RUN {
# 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)
# }

Run the code above in your browser using DataLab