Rocket Refs
Rocket Refs

Reputation: 27

Capitalizing in R with Exceptions

How can you capitalize data on R except add boundaries?

For example:

Given a list of cities and states in the form: "NEW YORK, NY" It needs to be changed to: "New York, NY"

The str_to_title function changes it to "New York, Ny".

Patterns: WASHINGTON, DC AMHERST, MA HANOVER, NH DAVIDSON, NC BRUNSWICK, ME GREENVILLE, SC PORTLAND, OR LOUISVILLE, KY

They should all be in the form: Amherst, MA or Brunswick, ME

Upvotes: 1

Views: 85

Answers (1)

akrun
akrun

Reputation: 887028

We could use a negative regex lookaround to match the upper case letters that are not succeeding the , and space , capture as a group ((...)), in the replacement specify the backreference of the captured group (\\1, \\2) while converting the second group to lower (\\L)

gsub("(?<!, )([A-Z])([A-Z]+)\\b", "\\1\\L\\2", str1, perl = TRUE)
#[1] "New York, NY"   "Washington, DC" "Amherst, MA"    "Hanover, NH" 
#[5]  "Davidson, NC"   "Brunswick, ME" 
#[7] "Greenville, SC" "Portland, OR"   "Louisville, KY"

data

str1 <- c("NEW YORK, NY", "WASHINGTON, DC", "AMHERST, MA", "HANOVER, NH", 
  "DAVIDSON, NC", "BRUNSWICK, ME", "GREENVILLE, SC", "PORTLAND, OR", 
 "LOUISVILLE, KY")

Upvotes: 1

Related Questions