First let’s load the library:
I often want to get the first, last or nth number in a string.
#> [1] 1000#> [1] 488#> [1] 512#> [[1]]
#> [1] "A population of " " comprised of "   " dogs and "      
#> [4] " cats."#> [[1]]
#> [1] "A population of " "1000"             " comprised of "  
#> [4] "488"              " dogs and "       "512"             
#> [7] " cats."Sometimes we don’t want to know is something is numeric, we want to know if it could be considered to be numeric (or could be coerced to numeric). For this, there’s can_be_numeric().
#> [1] TRUE#> [1] FALSE#> [1] TRUE#> [1] TRUE#> [1] FALSE#> [1] "b"#> [1] "z"stringr’s str_trim just trims whitespace. What if you want to trim something else? Now you can trim_anything().
#> [1] "rmarkdown"#> [1] 10#> [1] 2Suppose we want to remove double spacing:
double__spaced <- "Hello  world,  pretend  it's  Saturday  :-)"
count_matches(double__spaced, " ")  # count the spaces#> [1] 10#> [1] "Hello world, pretend it's Saturday :-)"#> [1] 5Suppose we have sentences telling us about a couple of boxes:
box_infos <- c("Box 1 has weight 23kg and volume 0.3 cubic metres.",
               "Box 2 has weight 20kg and volume 0.33 cubic metres.")We can get (for example) the weights of the boxes by taking the first number that appears after the word “weight”.
#> [1] " 23kg and volume 0.3 cubic metres." 
#> [2] " 20kg and volume 0.33 cubic metres."#> [1] 23 20We’d like to put all of the box information into a nice data frame. Here’s how.
tibble::tibble(box = nth_number(box_infos, 1),
        weight = str_after_nth(box_infos, "weight", 1) %>% 
          nth_number(1, decimals = TRUE),
        volume = str_after_nth(box_infos, "volume", 1) %>% 
          nth_number(1, decimals = TRUE)
)#> # A tibble: 2 x 3
#>     box weight volume
#>   <int>  <dbl>  <dbl>
#> 1     1     23   0.3 
#> 2     2     20   0.33Sometimes people use camel case (CamelCase) to avoid using spaces. What if we want to put the spaces back in?
#> [[1]]
#> [1] "Joe"    "Bloggs"
#> 
#> [[2]]
#> [1] "Janey" "Mac"