Introduction to R and Rstudio

Session - {stringr}

Zoë Turner

Wildcards searches using {stringr}

A natural step to searching for long strings is to consider searching by key words

library(dplyr)
library(stringr)
beds_data |>
  filter(str_detect(
    string = org_name,
    pattern = "Bradford",
    negate = FALSE
  ))

See what happens when negate is changed to TRUE

Adding trailing spaces

Quite often data has trailing spaces but using {readr}, interestingly, corrects this!

This data set has had trailing white space added to the beginning of the name and afterwards:

by_ethnicity <- tibble::tribble(
  ~Ethnicity, ~`%`, ~Headcount, ~`%.working.age.population.(2011)`,
     "  Asian  ",  10,     118396,                                7.2,
     "  Black ",  6.1,      72321,                                3.4,
   "    Chinese    ",  0.6,       6536,                                0.9,
     " Mixed  ",  1.7,      20607,                                1.8,
     " White ", 79.2,     934544,                               85.6,
     "Other",  2.3,      27169,                                1.1
  )

Removing trailing spaces

by_ethnicity |>
  mutate(trimmed_name = str_trim(Ethnicity, "both"))
# A tibble: 6 × 5
  Ethnicity           `%` Headcount %.working.age.population.(201…¹ trimmed_name
  <chr>             <dbl>     <dbl>                           <dbl> <chr>       
1 "  Asian  "        10      118396                             7.2 Asian       
2 "  Black "          6.1     72321                             3.4 Black       
3 "    Chinese    "   0.6      6536                             0.9 Chinese     
4 " Mixed  "          1.7     20607                             1.8 Mixed       
5 " White "          79.2    934544                            85.6 White       
6 "Other"             2.3     27169                             1.1 Other       
# ℹ abbreviated name: ¹​`%.working.age.population.(2011)`

End session