strex (version 1.4.1)

str_nth_number: Extract the nth number from a string.

Description

Extract the nth number from a string, where decimals, scientific notation and commas (as separators, not as an alternative to the decimal point) are optionally allowed.

Usage

str_nth_number(
  string,
  n,
  decimals = FALSE,
  leading_decimals = decimals,
  negs = FALSE,
  sci = FALSE,
  commas = FALSE,
  leave_as_string = FALSE
)

str_first_number( string, decimals = FALSE, leading_decimals = decimals, negs = FALSE, sci = FALSE, commas = FALSE, leave_as_string = FALSE )

str_last_number( string, decimals = FALSE, leading_decimals = decimals, negs = FALSE, sci = FALSE, commas = FALSE, leave_as_string = FALSE )

Arguments

string

A string.

n

A vector of integerish values. Must be either length 1 or have length equal to the length of string. Negative indices count from the back: while n = 1 and n = 2 correspond to first and second, n = -1 and n = -2 correspond to last and second-last. n = 0 will return NA.

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).

sci

Make the search aware of scientific notation e.g. 2e3 is the same as 2000.

commas

Allow comma separators in numbers (i.e. interpret 1,100 as a single number (one thousand one hundred) rather than two numbers (one and one hundred)).

leave_as_string

Do you want to return the number as a string (TRUE) or as numeric (FALSE, the default)?

Value

A numeric vector (or a character vector if leave_as_string = TRUE).

Details

  • str_first_number(...) is just str_nth_number(..., n = 1).

  • str_last_number(...) is just str_nth_number(..., n = -1).

For a detailed explanation of the number extraction, see str_extract_numbers().

See Also

Other numeric extractors: str_extract_numbers(), str_nth_number_after_mth(), str_nth_number_before_mth()

Examples

Run this code
# NOT RUN {
strings <- c(
  "abc123def456", "abc-0.12def.345", "abc.12e4def34.5e9",
  "abc1,100def1,230.5", "abc1,100e3,215def4e1,000"
)
str_nth_number(strings, n = 2)
str_nth_number(strings, n = -2, decimals = TRUE)
str_first_number(strings, decimals = TRUE, leading_decimals = TRUE)
str_last_number(strings, commas = TRUE)
str_nth_number(strings,
  n = 1, decimals = TRUE, leading_decimals = TRUE,
  sci = TRUE
)
str_first_number(strings,
  decimals = TRUE, leading_decimals = TRUE,
  sci = TRUE, commas = TRUE, negs = TRUE
)
str_last_number(strings,
  decimals = TRUE, leading_decimals = FALSE,
  sci = FALSE, commas = TRUE, negs = TRUE, leave_as_string = TRUE
)
str_first_number(c("22", "1.2.3"), decimals = TRUE)
# }

Run the code above in your browser using DataLab