Extract the nth number from a string, where decimals, scientific notation and thousand separators are optionally allowed.

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

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

str_last_number(
  string,
  decimals = FALSE,
  leading_decimals = decimals,
  negs = FALSE,
  sci = FALSE,
  big_mark = "",
  leave_as_string = FALSE,
  commas = 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.

big_mark

A character. Allow this character to be used as a thousands separator. This character will be removed from between digits before they are converted to numeric. You may specify many at once by pasting them together e.g. big_mark = ",_" will allow both commas and underscores. Internally, this will be used inside a [] regex block so e.g. "a-z" will behave differently to "az-". Most common separators (commas, spaces, underscores) should work fine.

leave_as_string

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

commas

Deprecated. Use big_mark instead.

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

Examples

strings <- c(
  "abc123def456", "abc-0.12def.345", "abc.12e4def34.5e9",
  "abc1,100def1,230.5", "abc1,100e3,215def4e1,000"
)
str_nth_number(strings, n = 2)
#> [1] 456  12   4 100 100
str_nth_number(strings, n = -2, decimals = TRUE)
#> [1] 123.00   0.12  34.50   1.00   1.00
str_first_number(strings, decimals = TRUE, leading_decimals = TRUE)
#> [1] 123.00   0.12   0.12   1.00   1.00
str_last_number(strings, big_mark = ",")
#> [1]  456  345    9    5 1000
str_nth_number(strings,
  n = 1, decimals = TRUE, leading_decimals = TRUE,
  sci = TRUE
)
#> [1]  123.00    0.12 1200.00    1.00    1.00
str_first_number(strings,
  decimals = TRUE, leading_decimals = TRUE,
  sci = TRUE, big_mark = ",", negs = TRUE
)
#> [1]  123.00   -0.12 1200.00 1100.00     Inf
str_last_number(strings,
  decimals = TRUE, leading_decimals = FALSE,
  sci = FALSE, big_mark = ",", negs = TRUE, leave_as_string = TRUE
)
#> [1] "456"     "345"     "9"       "1,230.5" "1,000"  
str_first_number(c("22", "1.2.3"), decimals = TRUE)
#> Warning: `NA`s introduced by ambiguity.
#>  The first such ambiguity is in string number 2 which is '1.2.3'.
#>  The offending part of that string is '.2.3'.
#> [1] 22 NA