currency {strex} | R Documentation |
Extract currency amounts from a string.
Description
The currency of a number is defined as the character coming before the number in the string. If nothing comes before (i.e. if the number is the first thing in the string), the currency is the empty string, similarly the currency can be a space, comma or any manner of thing.
Usage
str_extract_currencies(string)
str_nth_currency(string, n)
str_first_currency(string)
str_last_currency(string)
Arguments
string |
A character vector. |
n |
A vector of integerish values. Must be either length 1 or
have length equal to the length of |
Details
These functions are vectorized over string
and n
.
str_extract_currencies()
extracts all currency amounts.
str_nth_currency()
just gets the n
th currency amount from each string.
str_first_currency(string)
and str_last_currency(string)
are just
wrappers for str_nth_currency(string, n = 1)
and str_nth_currency(string, n = -1)
.
"-$2.00" and "$-2.00" are interpreted as negative two dollars.
If you request e.g. the 5th currency amount but there are only 3 currency
amounts, you get an amount and currency symbol of NA
.
Value
A data frame with 4 columns: string_num
, string
, curr_sym
and
amount
. Every extracted currency amount gets its own row in the data
frame detailing the string number and string that it was extracted from,
the currency symbol and the amount.
Examples
string <- c("ab3 13", "$1", "35.00 $1.14", "abc5 $3.8", "stuff")
str_extract_currencies(string)
str_nth_currency(string, n = 2)
str_nth_currency(string, n = -2)
str_nth_currency(string, c(1, -2, 1, 2, -1))
str_first_currency(string)
str_last_currency(string)