As an extension of the {vectrs}
package, a shrthnd_num()
is generally
coerced to behave as if it was a regular a numeric()
vector. Where
{vectrs}
doesn't automatically support coercion custom methods are
provided to enable a shrthnd_num()
to be considered as a numeric vector.
General principles
The principles underpinning the coercion of a shrthnd_num()
vector are
that to maximise compatability with base R and other packages, the vector
should generally behave as a numeric vector. This means that
as.numeric()
will produce a bare numeric vector containing just the
numeric component of a shrthnd_num()
. Similarly as.character()
will
produce a character vector of the numeric component of a shrthnd_num()
.
To work with tags use shrthnd_tags()
and the related
tag location functions. To produce a traditional
character vector combining the numeric component and tag component use
as_shrthnd()
on a shrthnd_num()
vector.
In keeping with base R practice around complex numeric objects such as
Date()
, difftime()
and POSIXct()
, using is.numeric()
on a
shrthnd_num()
vector will return FALSE
. Use is_shrthnd_num()
to test
if a vector is a shrthnd_num()
vector.
See shrthnd_maths
for details on how shrthnd_num()
works with
arithmetic, mathematical and (some) statistical operations.
Missing values
Of particular note is that using is.na()
on a shrthnd_num()
vector is
designed to work on the numeric component, i.e. if numeric component is
missing but a tag marker is present then is.na()
will return TRUE
. Use
is_na_tag()
to identify where there is no tag marker, or is_na_both()
to
identify where both the numeric and tag components are missing.
See also
as_shrthnd()
,
is_shrthnd_num()
,
make_shrthnd_num()
,
shrthnd_maths
,
shrthnd_num()
,
shrthnd_tags()
,
tag_match()
Examples
x <- c("12", "34.567", "[c]", "NA", "56.78[e]", "78.9", "90.123[e]")
sh_x <- shrthnd_num(x, c("[c]", "[e]"))
as.numeric(sh_x)
#> [1] 12.000 34.567 NA NA 56.780 78.900 90.123
as.character(sh_x)
#> [1] "12" "34.567" NA NA "56.78" "78.9" "90.123"
is.na(sh_x)
#> [1] FALSE FALSE TRUE TRUE FALSE FALSE FALSE