I have an aphorism that almost every time I reach for stringr
what I really want is tidyr::separate
. However, today I came up with what I thought was a simple example of string manipulation in class and ended up with a much more complicated solution than I would have liked. The idea is a variable with street names, and we want to pull off the "suffixes" (St, Dr, Ave, etc).
Here was my first (non-functional) attempt:
library(Stat2Data)
library(dplyr)
library(tidyr)
data("RailsTrails")
RailsTrails <- RailsTrails %>%
separate(StreetName, into = c("name", "kind"), sep = " ", extra = "merge")
Created on 2019-12-02 by the reprex package (v0.2.1)
This doesn't work, because of multi-word street names, so the first break goes into the first variable and the second two pieces are merged. Is there a way to modify this separate()
call so it does what I want?
Since I couldn't figure that out, I tried using str_split()
and ended up with this,
library(Stat2Data)
library(dplyr)
library(stringr)
library(purrr)
data("RailsTrails")
RailsTrails <- RailsTrails %>%
mutate(pieces = str_split(StreetName, " ")) %>%
mutate(last_piece = map_chr(pieces, ~.x[length(.x)]))
Created on 2019-12-02 by the reprex package (v0.2.1)
This works, but it uses map_chr()
, which I didn't intend to teach in this class! Any more elegant solutions out there?