selectElements: Select (and Rename) Elements from List

View source: R/list.R

selectElementsR Documentation

Select (and Rename) Elements from List

Description

select (and rename, if required) elements from list. Stop with message if elements do not exist

Usage

selectElements(x, elements = NULL, do.stop = TRUE, do.warn = TRUE)

Arguments

x

list

elements

vector of element names. The names of named elements will be the names in the output list

do.stop

this flag controls whether the function stops (do.stop = TRUE) or not (do.stop = FALSE) if there are non-existing elements to be selected. If do.stop = FALSE only those elements are selected that actually exist

do.warn

if TRUE (default) and do.stop = FALSE a warning is given if elements do not exist. Set to FALSE to suppress warnings

Value

list containing the elements of x that are specified in elements or x[[elements]] if length of elements is 1 or list() if elements is empty. If the elements in vector elements are named, these names are used in the output list.

Examples

L <- list(a = 1, b = 2, c = 3, d = 4)
  
# Select elements
selectElements(L, c("a", "c"))

# Select and rename at the same time
selectElements(L, elements = c(a.new = "a", c.new = "c", "b"))


KWB-R/kwb.utils documentation built on April 1, 2024, 7:12 a.m.