The features of strex
that were deemed the most
interesting have been given their own vignettes. However, the package
was intended as a miscellany of useful functions, so the functions
demonstrated here encapsulate the spirit of this package, i.e. functions
that save R string manipulators time.
Sometimes you don’t want to know whether something is numeric, just
whether or not it could be. Now you can find out with
str_can_be_numeric()
.
To get currencies and amounts mentioned in strings, there are
str_extract_currencies()
and
str_nth_currency()
, str_first_currency()
and
str_last_currency()
. str_first_currency()
just
returns the first currency amount. str_last_currency()
returns the last. str_nth_currency()
allows you to get the
second, third and so on. str_extract_currencies()
returns
all currency amounts mentioned in a string.
string <- c("Alan paid £5", "Joe paid $7")
str_first_currency(string)
#> string_num string curr_sym amount
#> 1 1 Alan paid £5 £ 5
#> 2 2 Joe paid $7 $ 7
string <- c("€1 is $1.17", "£1 is $1.29")
str_nth_currency(string, n = c(1, 2))
#> string_num string curr_sym amount
#> 1 1 €1 is $1.17 € 1.00
#> 2 2 £1 is $1.29 $ 1.29
str_last_currency(string) # only gets the first mentioned
#> string_num string curr_sym amount
#> 1 1 €1 is $1.17 $ 1.17
#> 2 2 £1 is $1.29 $ 1.29
str_extract_currencies(string)
#> string_num string curr_sym amount
#> 1 1 €1 is $1.17 € 1.00
#> 2 1 €1 is $1.17 $ 1.17
#> 3 2 £1 is $1.29 £ 1.00
#> 4 2 £1 is $1.29 $ 1.29
This is a simple wrapper around stringr::str_sub()
.
We can give files a given extension, leaving them alone if they already have it.
string <- c("spreadsheet1.csv", "spreadsheet2")
str_give_ext(string, "csv")
#> [1] "spreadsheet1.csv" "spreadsheet2.csv"
If the file already has an extension, we can append one or replace it.
I’m not mad on CamelCase, I often want to deconstruct it.
This is something I did a lot to avoid using regular expression. Don’t do it for that purpose. Learn regex. https://regexone.com/ is a very good start.
What if something is needlessly surrounded by parentheses and we want to get rid of them?