Learn R Programming

container (version 1.0.5)

peek_at: Peek at Indices

Description

Try to access elements and return default values if not found. In contrast to [at()], this function provides a less stricter element access, that is, it remains valid even if elements don't exist.

Usage

peek_at(.x, ...)

# S3 method for Container peek_at(.x, ..., .default = NULL)

# S3 method for dict.table peek_at(.x, ..., .default = NULL)

Value

For Container, returns the value at the given indices or (if not found) the given default value.

For dict.table, returns the columns at the given indices or (if not found) columns with the given default value.

Arguments

.x

an R object of the respective class.

...

indices of elements to be extracted

.default

value to be returned if peeked value does not exist.

Details

peek_at tries to access specific values.

See Also

at() for strict element extraction

Examples

Run this code

# Container
co = container(a = 1, 2, b = 3, 4)
peek_at(co, 1)
peek_at(co, "a")
peek_at(co, "x")
peek_at(co, "x", .default = 0)
peek_at(co, "a", "x", 2, 9, .default = -1)

# Dict
d = dict(a = 1, b = 1:3)
peek_at(d, "b")
peek_at(d, "x")
peek_at(d, "x", .default = 4:7)

# dict.table
dit = dict.table(a = 1:3, b = 4:6)
peek_at(dit, "a")
peek_at(dit, 1)
peek_at(dit, 3)
peek_at(dit, "x")
peek_at(dit, "x", .default = 0)
peek_at(dit, "a", "x", .default = 0)

Run the code above in your browser using DataLab