Simple yet useful wrappers for working on strings. Most of them are built on top of existing stringr functions ; not all return strings.

str_remove_useless_spaces(x)

str_underscorise(x, pattern = "-+|[[:space:]]+")

str_trim_ext(x, pattern = "\\.[[:alnum:]]+$")

str_trim_dirpath(x, pattern = "^.*/")

str_split_on(x, pattern)

str_to_mat(x)

str_drop(x, pattern)

str_to_named_list(x, pattern)

Arguments

x

character

pattern

regex (a regular expression)

Value

character

Functions

  • str_remove_useless_spaces: remove empty lines, turn tabs into spaces and remove multiple spaces

  • str_underscorise: turn dashes and spaces into underscores

  • str_trim_ext: trim extension

  • str_trim_dirpath: trim dirpath

  • str_split_on: given a string, split on pattern

  • str_to_mat: turn coordinates as vector of strings into a matrix

  • str_drop: drop elements with pattern

  • str_to_named_list: turn a string with pattern on a named list

See also

To extract information from paths see extract_path

To fix information from paths, besides these ones, you have plenty friends in stringr, and particularly:

Other path: extract_path

Examples

messy_paths
#> [1] "Big_one/folder-foo/file_n.jpg" "folder-foo/File n.jpg" #> [3] "folder-foo2/file 5.jpg" "file.txt" #> [5] "file.jpeg" "file.png" #> [7] "file.mom"
messy_paths %>% str_underscorise()
#> [1] "Big_one/folder_foo/file_n.jpg" "folder_foo/File_n.jpg" #> [3] "folder_foo2/file_5.jpg" "file.txt" #> [5] "file.jpeg" "file.png" #> [7] "file.mom"