Base R's matching and location functions will work directly with the
numeric component of a shrthnd_num() vector, these functions provide
the same functionality but applied to the tag component.
Usage
tag_match(x, tag)
tag_in(x, tag)
where_tag(x, tag)
any_tag(x)
is_na_tag(x)
is_na_both(x)
locate_tag(x, tag)
locate_any_tag(x)
locate_no_tag(x)Arguments
- x
A
shrthnd_num()vector- tag
A single tag to locate
Value
For tag_match(), locate_tag(), locate_any_tag() and
locate_no_tag() an integer vector. For tag_in(), where_tag(),
any_tag(), is_na_tag() and is_na_both() a logical vector.
Details
tag_match() and tag_in() are wrappers around vctrs::vec_match() and
vctrs::vec_in() and thus equivalent to match() and %in% as applied
to the tag components of a shrthnd_num(). tag_match() will return an
integer vector showing the first location of the tag provided, tag_in()
will return TRUE or FALSE depending on whether the tag is in the
vector's shorthand.
where_tag() is equivalent to computing tags == tag, any_tag() is
equivalent to !is.na(tags). Using is.na() on a shrthnd_num() will
assess if the numeric component is missing, is_na_tag() is equivalent to
is.na(tags), is_na_both() tests if both the numeric and tag components
of a shrthnd_num() are missing. They return a logical
vector the same length as x.
locate_tag(), locate_any_tag(), located_no_tag() are equivalent to
passing the return values of where_tag(), any_tag() and is_na_tag() to
which(). They return an integer vector the same length as x.
Examples
x <- c("12", "34.567", "[c]", "NA", "56.78[e]", "78.9", "90.123[e]")
sh_x <- shrthnd_num(x, c("[c]", "[e]"))
shrthnd_tags(sh_x)
#> [1] NA NA "[c]" NA "[e]" NA "[e]"
tag_match(sh_x, "[e]")
#> [1] 5
tag_in(sh_x, "[e]")
#> [1] TRUE
where_tag(sh_x, "[e]")
#> [1] NA NA FALSE NA TRUE NA TRUE
any_tag(sh_x)
#> [1] FALSE FALSE TRUE FALSE TRUE FALSE TRUE
is_na_tag(sh_x)
#> [1] TRUE TRUE FALSE TRUE FALSE TRUE FALSE
is_na_both(sh_x)
#> [1] FALSE FALSE FALSE TRUE FALSE FALSE FALSE
locate_tag(sh_x, "[e]")
#> [1] 5 7
locate_any_tag(sh_x)
#> [1] 3 5 7
locate_no_tag(sh_x)
#> [1] 1 2 4 6