| str_extract_numbers {strex} | R Documentation |
str_extract_numbers extracts the numbers (or non-numbers) from a string
where decimals are optionally allowed. str_nth_number is a convenient
wrapper for str_extract_numbers, 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.
str_extract_numbers(string, leave_as_string = FALSE, decimals = FALSE, leading_decimals = FALSE, negs = FALSE) str_nth_number(string, n, leave_as_string = FALSE, decimals = FALSE, leading_decimals = FALSE, negs = FALSE) str_first_number(string, leave_as_string = FALSE, decimals = FALSE, leading_decimals = FALSE, negs = FALSE) str_last_number(string, leave_as_string = FALSE, decimals = FALSE, leading_decimals = FALSE, negs = FALSE)
string |
A string. |
leave_as_string |
Do you want to return the number as a string ( |
decimals |
Do you want to include the possibility of decimal numbers
( |
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. |
If any part of a string contains an ambiguous number (e.g. 1.2.3 would be
ambiguous if decimals = TRUE (but not otherwise)), the value returned for
that string will be NA. Note that these functions do not know about
scientific notation (e.g. 1e6 for 1000000).
str_first_number(...) is just str_nth_number(..., n = 1).
str_last_number(...) is just str_nth_number(..., n = -1).
For str_extract_numbers and extract_non_numerics, a list of
numeric or character vectors, one list element for each element of
string. For str_nth_number and nth_non_numeric, a vector the same
length as string (as in length(string), not nchar(string)).
str_extract_numbers(c("abc123abc456", "abc1.23abc456"))
str_extract_numbers(c("abc1.23abc456", "abc1..23abc456"), decimals = TRUE)
str_extract_numbers("abc1..23abc456", decimals = TRUE)
str_extract_numbers("abc1..23abc456", decimals = TRUE, leading_decimals = TRUE)
str_extract_numbers("abc1..23abc456", decimals = TRUE, leading_decimals = TRUE,
leave_as_string = TRUE)
str_extract_numbers("-123abc456")
str_extract_numbers("-123abc456", negs = TRUE)
str_extract_numbers("--123abc456", negs = TRUE)
str_extract_numbers(c(rep("abc1.2.3", 2), "a1b2.2.3", "e5r6"), decimals = TRUE)
str_extract_numbers("ab.1.2", decimals = TRUE, leading_decimals = TRUE)
str_nth_number("abc1.23abc456", 2:3)
str_nth_number("abc1.23abc456", 2, decimals = TRUE)
str_nth_number("-123abc456", -2, negs = TRUE)