title: Extract city names from large text with R
body: <p>Hello i have an intriguing question here. Suppose that i have a long character which includes city names between others.</p>
  
  <pre><code>test&lt;-"Ucsd Medical Center, San Diego, California, USA|Yale Cancer Center, New Haven, Connecticut, USA|Massachusetts General Hospital., Boston, Massachusetts, USA|Dana Farber Cancer Institute, Boston, Massachusetts, USA|Washington University, Saint Louis, Missouri, USA|Mount SInai Medical Center, New York, New York, USA|Memorial Sloan Kettering Cancer Center, New York, New York, USA|Carolinas Healthcare System, Charlotte, North Carolina, USA|University Hospitals Case Medical Center; Seidman Cancer Center, Cleveland, Ohio, USA|Vanderbilt University Medical Center, Nashville, Tennessee, USA|Seattle Cancer Care Alliance, Seattle, Washington, USA|National Cancer Center, Gyeonggi-do, Korea, Republic of|Seoul National University Hospital, Seoul, Korea, Republic of|Severance Hospital, Yonsei University Health System, Seoul, Korea, Republic of|Korea University Guro Hospital, Seoul, Korea, Republic of|Asan Medical Center., Seoul, Korea, Republic of|VU MEDISCH CENTRUM; Dept. of Medical Oncology"
  </code></pre>
  
  <p>My goal is to extract all the city names of it. And I achieved it by following five steps. </p>
  
  <pre><code>   #replace | with ,
     test2&lt;-str_replace_all(test, "[|]", ", ")
  
     # Remove punctuation from data
     test3&lt;-gsub("[[:punct:]\n]","",test2)
  
     # Split data at word boundaries
     test4 &lt;- strsplit(test3, " ")
  
     # Load data from package maps
     data(world.cities)
  
     # Match on cities in world.cities
     citiestest&lt;-lapply(test4, function(x)x[which(x %in% world.cities$name)])
  </code></pre>
  
  <p>The result may be correct</p>
  
  <pre><code>citiestest
  [[1]]
   [1] "San"        "Boston"     "Boston"     "Washington" "York"      
   [6] "York"       "Kettering"  "York"       "York"       "Charlotte" 
  [11] "Carolina"   "Cleveland"  "Nashville"  "Seattle"    "Seattle"   
  [16] "Washington" "Asan"      
  </code></pre>
  
  <p>But as you can see I cannot deal with cities with two-words name (New York, San Diego etc.) as they are separated. Of course fix this issue manually is not an option as my real dataset is quite large.</p>