I've looked at several SO posts but I'm at the "beating head against tree" stage. I appreciate your time.
I have a dataframe (about 300 cases) with a text string; I simply want to scan a separate list of cities (7000 of them), and if city in the string matches the list, I want a new data frame column to be written with the matching city name.
My data:
df<-structure(list(Item = c("1965 Wilkes College, Wilkes-Barre, PA", "1967 Spanish National Tourist Office, New York City", "1968 William Penn Memorial Museum, Harrisburg, PA",
"2010 Strange Evidence. Philadelphia Museum of Art. Peter Barbarie, Curator.","1973 The Museum of Modern Art, New York City", "1974 International Museum of Photography, George Eastman House, Rochester, NY", "1974 Light Gallery, New York City", "1975 Art Institute of Chicago"
)), row.names = c(NA, -8L), class = c("tbl_df", "tbl", "data.frame"
))
citylist<-c("Barre","Sacramento","Palmer", "New York City","Chicago","Rochester")
I've tried things like:
df$city<-sapply(df$Item,function(x) df$Item[df$Item %in% citylist])
Or
citymatcher<-function(x){match(x,citylist)}
df$city<-sapply(df$Item,citymatcher)
Ultimately, I want a tidy dataframe with a new column indicating the city that matches a given row.
