ex: Extract Parts of an Object with Varnames and Units Attributes

View source: R/Data_handling.R

exR Documentation

Extract Parts of an Object with Varnames and Units Attributes

Description

Conserves varnames and units attributes of vectors and data frames during extraction.

Usage

ex(x, i, j, drop = TRUE)

Arguments

x

An atomic type or a data frame. Object from which to extract element(s).

i, j

Indices specifying elements to extract as specified in Extract.

drop

A logical value. If TRUE (default), the result is coerced to the lowest possible dimension.

Details

Extraction from atomic types is done as x[i] ignoring j and drop (applies also to matrices and arrays). Extraction from data frames is done as x[i, j, drop].

Value

A vector or data frame with varnames and units attributes.

See Also

Extract, drop and varnames.

Examples

xx <- data.frame(lengths = 1:3, areas = 4:6)
varnames(xx) <- c("lengths", "areas")
units(xx) <- c("m", "m2")
str(xx)

# extract specified rows and columns
str(ex(xx, 1:2, 1:2))
# extract specified rows
str(ex(xx, 1))
# extract specified columns
str(ex(xx, , 1))
# extract without dropping current class
ex(xx, , 1, drop = FALSE)
# extract elements of a vector
ex(xx$lengths, 2:3)


lsigut/openeddy documentation built on Aug. 5, 2023, 12:25 a.m.