Arithmetic and most mathematical operations are supported on the numeric
component of shrthnd_num()
vectors via the {vctrs}
package without
having to wrap the vector in as.numeric()
.
Details
You can use all the standard arithmetic infix operators (+
, -
, /
, *
,
^
, %%
, %/%
, !
). See vctrs::vec_arith()
for further details.
Through vctrs::vec_math()
the following generic mathematical operations
are supported:
from the Summary group generic:
prod()
,sum()
,any()
,all()
.from the Math group generic:
abs()
,sign()
,sqrt()
,ceiling()
,floor()
,trunc()
,cummax()
,cummin()
,cumprod()
,cumsum()
,log()
,log10()
,log2()
,log1p()
,acos()
,acosh()
,asin()
,asinh()
,atan()
,atanh()
,exp()
,expm1()
,cos()
,cosh()
,cospi()
,sin()
,sinh()
,sinpi()
,tan()
,tanh()
,tanpi()
,gamma()
,lgamma()
,digamma()
,trigamma()
.vctrs::vec_math()
also enables support formean()
,is.nan()
,is.finite()
andis.infinite()
.In addition to these, the
{shrthnd}
package also provides methods forrange()
,min()
,max()
,median()
andquantile()
. Ashrthnd_num()
will work withsd()
due to the ability of ashrthnd_num()
to be easily coerced to a numeric vector.
For other operations you will need to wrap the shrthnd_num
vector in
as.numeric()
.
For all operations remember that you will likely need to set na.rm = TRUE
or whatever other method a function has for ignoring missing values.
See also
as_shrthnd()
,
is_shrthnd_num()
,
make_shrthnd_num()
,
shrthnd_coercion
,
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]"))
sh_x * 2
#> [1] 24.000 69.134 NA NA 113.560 157.800 180.246
2 + sh_x
#> [1] 14.000 36.567 NA NA 58.780 80.900 92.123
mean(sh_x, na.rm = TRUE)
#> [1] 54.474