I have a df of addresses for schools for multiple years, but due to inconsistent data input, some schools have addresses written differently between years.
I am trying to fix them by using str_replace to change things like "St." into "Street", but sometimes I need to specify that the pattern is the end of the character vector. For example "West St" should become "West Street", but it I replace "St" with "Street", then the ones written correctly become "West Streetreet".
test <- tribble(
~ name, ~ year, ~address,
"school 1", 2000, "1 Main Ave",
"school 1", 2001, "1 Main Avenue",
"school 1", 2002, "1 Main Ave",
"school 1", 2004, "1 Main Avenue",
"school 2", 2000, "200 West St",
"school 2", 2001, "200 West Street",
"school 2", 2002, "200 West St",
"school 2", 2004, "200 West St",
"school 3", 2000, "2759 Lakeshore Road",
"school 3", 2001, "2759 Lakeshore Road",
"school 3", 2002, "2759 Lakeshore Rd",
"school 3", 2004, "2759 Lakeshore Rd"
)
test %>%
mutate(address2 = str_replace(address, "Rd","Road"),
address2 = str_replace(address2, "Ave","Avenue"),
address2 = str_replace(address2, "St","Street"))
this returns:
# A tibble: 12 × 4
name year address address2
<chr> <dbl> <chr> <chr>
1 school 1 2000 1 Main Ave 1 Main Avenue
2 school 1 2001 1 Main Avenue 1 Main Avenuenue
3 school 1 2002 1 Main Ave 1 Main Avenue
4 school 1 2004 1 Main Avenue 1 Main Avenuenue
5 school 2 2000 200 West St 200 West Street
6 school 2 2001 200 West Street 200 West Streetreet
7 school 2 2002 200 West St 200 West Street
8 school 2 2004 200 West St 200 West Street
9 school 3 2000 2759 Lakeshore Road 2759 Lakeshore Road
10 school 3 2001 2759 Lakeshore Road 2759 Lakeshore Road
11 school 3 2002 2759 Lakeshore Rd 2759 Lakeshore Road
12 school 3 2004 2759 Lakeshore Rd 2759 Lakeshore Road
which is obviously not correct. How do I specify that it's only when a pattern ENDS with "St", that it should be changed?