strex (version 0.1.3)

str_extract_non_numerics: Extract non-numbers from a string.

Description

str_extract_non_numerics extracts the bits of the string that aren't extracted by extract_numbers. str_nth_non_numeric is a convenient wrapper for str_extract_non_numerics, allowing you to choose which number you want. Please run the examples at the bottom of this page to ensure that you understand how these functions work, and their limitations. These functions are vectorized over string.

Usage

str_extract_non_numerics(string, decimals = FALSE,
  leading_decimals = FALSE, negs = FALSE)

str_nth_non_numeric(string, n, decimals = FALSE, leading_decimals = FALSE, negs = FALSE)

str_first_non_numeric(string, decimals = FALSE, leading_decimals = FALSE, negs = FALSE)

str_last_non_numeric(string, decimals = FALSE, leading_decimals = FALSE, negs = FALSE)

Arguments

string

A string.

decimals

Do you want to include the possibility of decimal numbers (TRUE) or not (FALSE, the default).

leading_decimals

Do you want to allow a leading decimal point to be the start of a number?

negs

Do you want to allow negative numbers? Note that double negatives are not handled here (see the examples).

n

The index of the number (or non-numeric) that you seek. Negative indexing is allowed i.e. n = 1 (the default) will give you the first number (or non-numeric) whereas n = -1 will give you the last number (or non-numeric), n = -2 will give you the second last number and so on. The function is vectorized over this argument.

Details

  • str_first_non_numeric(...) is just str_nth_non_numeric(..., n = 1).

  • str_last_non_numeric(...) is just str_nth_non_numeric(..., n = -1).

Examples

Run this code
# NOT RUN {
str_extract_non_numerics("abc123abc456")
str_extract_non_numerics("abc1.23abc456")
str_extract_non_numerics("abc1.23abc456", decimals = TRUE)
str_extract_non_numerics("abc1..23abc456", decimals = TRUE)
str_extract_non_numerics("abc1..23abc456", decimals = TRUE,
                     leading_decimals = TRUE)
str_extract_non_numerics(c("-123abc456", "ab1c"))
str_extract_non_numerics("-123abc456", negs = TRUE)
str_extract_non_numerics("--123abc456", negs = TRUE)
str_extract_non_numerics("--123abc456", negs = TRUE)
str_nth_non_numeric("--123abc456", 1)
str_nth_non_numeric("--123abc456", -2)

# }

Run the code above in your browser using DataCamp Workspace