Hi,
I have this list of train stations:
source <- data.frame(
stringsAsFactors = FALSE,
Station = c("Acton Central",
"Aldgate East","Allens West",
"Armadale (West Lothian)","Bicester North",
"Birkenhead Central","Birkenhead North",
"Blackpool North",
"Bloxwich North","Box Hill and Westhumble",
"Bramley (West Yorkshire)",
"Bromley North","Burnley Central",
"Cambridge North","Canterbury East",
"Canterbury West","Cardiff Central","Centrale"),
PostCode = c("W","E","TS","EH",
"MK","CH","CH","FY","WS","KT",
"BD","BR","BB","CB","CT","CT",
"CF","CR")
)
source
and I would like to generate towns they are in by removing North, South, West, East or Central from their names with the exception of "Centrale" and stations with locations in brackets.
As a result I would need this:
result <- data.frame(
stringsAsFactors = FALSE,
Station = c("Acton Central",
"Aldgate East","Allens West",
"Armadale (West Lothian)","Bicester North",
"Birkenhead Central","Birkenhead North",
"Blackpool North",
"Bloxwich North","Box Hill and Westhumble",
"Bramley (West Yorkshire)",
"Bromley North","Burnley Central",
"Cambridge North","Canterbury East",
"Canterbury West","Cardiff Central","Centrale"),
PostCode = c("W","E","TS","EH",
"MK","CH","CH","FY","WS","KT",
"BD","BR","BB","CB","CT","CT",
"CF","CR"),
Town = c("Acton","Aldgate",
"Allens","Armadale (West Lothian)",
"Bicester","Birkenhead","Birkenhead",
"Blackpool","Bloxwich",
"Box Hill and Westhumble",
"Bramley (West Yorkshire)","Bromley","Burnley",
"Cambridge","Canterbury","Canterbury",
"Cardiff","Centrale")
)
result
Shall I use a modified version of something like that to remove the words?
blank_statements <- regex("^(North|South|West|East|Central)$", ignore_case = TRUE)
result <- source %>%
mutate(across(contains("Station"), ~str_trim(.x))) %>%
mutate(across(contains("Station"), ~str_remove_all(.x, blank_statements))
)
How can I keep the exceptions?
Can you help?