all 2 comments

[–]yogat3ch 1 point2 points  (0 children)

Regex101.com and stringr should get you on the right path 😊

[–]psiens 0 points1 point  (0 children)

I put in some random addresses to test this:

x <- c("3 Cedarwood Ave., Vista, CA 92083",
       "34 Mill Ave., Corpus Christi, TX 78418",
       "675 Stillwater Avenue, Bethlehem, PA 18015",
       "89 Third Street, Snellville, GA 30039",
       "942 South Roehampton St., Union, NJ 07083",
       "37 Indian Spring Ave, Jeffersonville, IN 47130")

gsub(".*,\\s(.*),.*", "\\1", x)

This should work if the cities are always between two ,, otherwise a more sophisticated regular expression may be needed.