Learn R Programming

container (version 1.0.5)

peek_at2: Peek at Single Index

Description

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

Usage

peek_at2(x, index, default = NULL)

# S3 method for Container peek_at2(x, index, default = NULL)

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

Value

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

For dict.table, returns the column named index if it exist otherwise the given default value. If the default length does not match the number of rows, it is recycled accordingly and a warning is given, unless the default value has a length of 1, in which case recycling is done silently.

Arguments

x

an R object of the respective class.

index

character name or numeric position of the sought value.

default

value to be returned if peeked value does not exist.

See Also

at2() for strict element extraction

Examples

Run this code

# Container
co = container(a = 1, 2, b = 3, 4)
peek_at2(co, 1)
peek_at2(co, "a")
peek_at2(co, "x")
peek_at2(co, "x", default = 0)

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

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

Run the code above in your browser using DataLab