Skip to contents

shrthnd_num() coerces a character vector containing numeric data values with non-numeric tags into a numeric-like vector while also retaining the tags.

Usage

shrthnd_num(
  x,
  shorthand = NULL,
  na_values = c("", "NA"),
  digits = 2L,
  paren_nums = c("negative", "strip"),
  dec = ".",
  bigmark = ",",
  convert_percent = TRUE
)

Arguments

x

A character vector of numeric values with shorthand

shorthand

A character vector of shorthand values

na_values

A character vector of NA values

digits

The number of digits for formatting numbers

paren_nums

How to handle numbers in parenthesis (e.g. (12,435.43)), defaults to negative as most commonly used in accounting to denote negative values instead of a minus symbol preceding the value

dec

The decimal separator for numbers

bigmark

The separator to the left of the decimal separator

convert_percent

Whether to convert percentages into decimals

Value

A shrthnd_num vector

Details

Data stored in documents and publications are regularly annotated with shorthand and symbols. Often these tags are found in the same container (e.g. a table or spreadsheet cell) as the value they are associated with, which requires further cleaning of the vector to extract the numeric values.

A simple approach is to discard the non-numeric components, however these tags can convey information which you may wish to retain. shrthnd_num() provides a data type that can store both the numeric data and the marker.

By default shrthnd_num() will extract any non-numeric values following numeric ones and process them as a shorthand tag. However, you can optionally supply a vector of tags, using the shorthand argument, if you wish to validate the extracted tags and only accept vectors with specific shorthand values.

If the underlying numeric values are real numbers (i.e. a double() vector) the digits argument will be used to format the display of the shrthnd_dbl vector (defaults to 2 decimal places).

Examples

x <- c("12", "34.567", "[c]", "NA", "56.78[e]", "78.9", "90.123[e]")
shrthnd_num(x, c("[c]", "[e]"))
#> <shrthnd_num[7]>
#> [1] 12.00     34.57        NA [c]    NA     56.78 [e] 78.90     90.12 [e]