Extracts the value of a Container at the given index. If the index is invalid, an error is signaled. If given as a string, the element matching the name is returned. If there are two or more identical names, the value of the first match (i.e. leftmost element) is returned. Extract value at index. If index is invalid or not found, an error is signaled. If given as a string, the element matching the name is returned. If there are two or more identical names, the value of the first match (i.e. leftmost element) is returned.
Usage
at2(x, ...)
# S3 method for class 'Container'
at2(x, index, ...)
# S3 method for class 'dict.table'
at2(x, index, ...)
Value
For Container
, returns the value at the given index.
For dict.table
, returns the column at the given index
or signals
an error if not found.
See also
peek_at2()
for less strict extraction
Examples
# Container
co = container(a = 1, 2, b = 3, 4)
at2(co, 1)
#> [1] 1
at2(co, "a")
#> [1] 1
at2(co, 2)
#> [1] 2
try(at2(co, "x")) # index 'x' not found
#> Error : index 'x' not found
try(at2(co, 5)) # index 5 exceeds length of Container
#> Error : index 5 exceeds length of Container, which is 4
# Dict
d = dict(a = 1, b = 3)
at2(d, 1)
#> [1] 1
at2(d, "a")
#> [1] 1
at2(d, 2)
#> [1] 3
try(at2(d, "x")) # index 'x' not found
#> Error : index 'x' not found
try(at2(d, 5)) # index 5 exceeds length of Dict
#> Error : index 5 exceeds length of Dict, which is 2
# dict.table
dit = dict.table(a = 1:3, b = 4:6)
at2(dit, 1)
#> [1] 1 2 3
at2(dit, "a")
#> [1] 1 2 3
at2(dit, 2)
#> [1] 4 5 6
try(at2(dit, "x")) # index 'x' not found
#> Error : index 'x' not found
try(at2(dit, 5)) # index 5 exceeds length of dict.table
#> Error : index 5 exceeds length of dict.table, which is 2