title: Extracting elements from a string
body: <p>Let's say I have the following datasets where the columns are structured as follows.</p>
  
  <pre><code>df1 = data.frame(Date=c(rnorm(5)),  
                   "United States) New York (NY" = c(rnorm(5)), 
                   "United States) Chicago (Illinois" = c(rnorm(5)),
                   "United States) Denver (Colorado" = c(rnorm(5)),
                   "United States) Seattle (Washington" = c(rnorm(5)),
                   "United States) Minneapolis (Minnesota" = c(rnorm(5)), check.names=FALSE)
  df1
  
  df2 = data.frame(Date=c(rnorm(5)),
                   "New York (New York, United States)" = c(rnorm(5)),
                   "Phoenix (Arizona, United States)" = c(rnorm(5)),
                   "Chicago (Illinois, United States)" = c(rnorm(5)),
                   "Los Angeles (California, United States)" = c(rnorm(5)), check.names=FALSE)
  df2
  </code></pre>
  
  <p>As you can see, each column is meant to represent a city, but the structure of the column name is not manageable. I was wondering if anyone could help me figure out how to extract the city name from the column name string.</p>
  
  <p>I could have a dictionary of each city and do a string match, but I've had little luck with that. I also assumed there'd be a way to do this with str_split but I haven't figured it out yet.</p>
  
  <pre><code>sapply(str_split(names(df1),")"), 2)
  </code></pre>
  
  <p>Of course, I'm sure there's a gsub solution also but I'm a little inept when it comes to regular expressions.</p>
  
  <p>Ultimately, I just want the actual city name as a column name.</p>
  
  <pre><code>New York, Chicago, Denver, Seattle, Minneapolis
  </code></pre>