filesstrings (version 0.3.2)

ExtractNumbers: Extract numbers (or non-numbers) from a string.

Description

ExtractNumbers extracts the numbers (or non-numbers) from a string where decimals are optionally allowed. ExtractNonNumerics extracts the bits of the string that aren't extracted by ExtractNumbers. NthNumber is a convenient wrapper for ExtractNumbers, allowing you to choose which number you want. Similarly NthNonNumeric. Please view the examples at the bottom of this page to ensure that you understand how these functions work, and their limitations. These functions are vectorised over string.

Usage

ExtractNumbers(string, leave.as.string = FALSE, decimals = FALSE,
  leading.decimals = FALSE, negs = FALSE)

ExtractNonNumerics(string, decimals = FALSE, leading.decimals = FALSE, negs = FALSE)

NthNumber(string, n, leave.as.string = FALSE, decimals = FALSE, leading.decimals = FALSE, negs = FALSE)

NthNonNumeric(string, n, decimals = FALSE, leading.decimals = FALSE, negs = FALSE)

Arguments

string
A string.
leave.as.string
Do you want to return the number as a string (TRUE) or as numeric (FALSE, the default)?
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 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.

Value

For ExtractNumbers and ExtractNonNumerics, a list of numeric or character vectors, one list element for each element of string. For NthNumber and NthNonNumeric, a vector the same length as string (as in length(string), not nchar(string)).

Details

ExtractNonNumerics uses ExtractNumerics to tell it what the numbers in the string are and then it extracts the bits in between those numbers. For this reason, errors you see whilst using ExtractNonNumerics might be errors from ExtractNumerics.

Examples

Run this code
ExtractNumbers(c("abc123abc456", "abc1.23abc456"))
ExtractNumbers(c("abc1.23abc456", "abc1..23abc456"), decimals = TRUE)
ExtractNumbers("abc1..23abc456", decimals = TRUE)
ExtractNumbers("abc1..23abc456", decimals = TRUE, leading.decimals = TRUE)
ExtractNumbers("abc1..23abc456", decimals = TRUE, leading.decimals = TRUE,
leave.as.string = TRUE)
ExtractNumbers("-123abc456")
ExtractNumbers("-123abc456", negs = TRUE)
ExtractNumbers("--123abc456", negs = TRUE)
ExtractNonNumerics("abc123abc456")
ExtractNonNumerics("abc1.23abc456")
ExtractNonNumerics("abc1.23abc456", decimals = TRUE)
ExtractNonNumerics("abc1..23abc456", decimals = TRUE)
ExtractNonNumerics("abc1..23abc456", decimals = TRUE,
leading.decimals = TRUE)
ExtractNonNumerics(c("-123abc456", "ab1c"))
ExtractNonNumerics("-123abc456", negs = TRUE)
ExtractNonNumerics("--123abc456", negs = TRUE)
ExtractNumbers("abc1.2.3", decimals = TRUE)
ExtractNumbers("ab.1.2", decimals = TRUE, leading.decimals = TRUE)
NthNumber("abc1.23abc456", 2)
NthNumber("abc1.23abc456", 2, decimals = TRUE)
NthNumber("-123abc456", -2, negs = TRUE)
ExtractNonNumerics("--123abc456", negs = TRUE)
NthNonNumeric("--123abc456", 1)
NthNonNumeric("--123abc456", -2)

Run the code above in your browser using DataCamp Workspace