Question: I have a dataframe "cities.df" that contains in one column all cities of Brazil and in the other column its respective state.

In the other dataframe "tweets.df", I have a column with tweets, that might contain some reference about a city or a state.

What I'm trying to do is to add a column to tweets.df with a "estimation" of its location, assuming that if he tweeted something about a city in the state of São Paulo, he is located in the state of São Paulo.

I'm new to R, and the only way I can think of how to implement this is with a cascaded loop over all tweets and then over all cities applying a grep function. But this does not seems like the correct way to implement it with R.

```
for tweet in tweets
    for city in cities
        grep(city, tweet)

```

I wonder if there is a more "vectorizable" way to implement this. 

Thanks

Answer: 
Here's one way you could do it. Consider `cities` and `tweets` to be vectors pulled from their respective data frames. The tweets are obviously made-up and are not even words. I just created them by sampling letters and pasting them together. To be updated if you add some data. 

The last line will return the tweets that contain a city from the `cities` list.

```
> cities
# [1] "Belém"               "Belo Horizonte"      "Blumenau"           
# [4] "Brasília"            "Campinas"            "Curitiba"           
# [7] "Florianópolis"       "Fortaleza"           "Goiania"            
# [10] "Macáe"               "Manaus"              "Niteroi"            
# [13] "Porto Alegre"        "Recife"              "Rio de Janeiro"     
# [16] "Roraima"             "Salvador"            "Santo Andre"        
# [19] "Santos"              "São José dos Campos" "São Paulo"          
# [22] "Vitória"            
> tweets
# [1] "Niteroi rjzzfbymh wj fa elxbmyfk logeb"
# [2] "no city present in this sentence"      
# [3] "Vitória zl qxllds buoo fvclizxv zqf"   
# [4] "Rio de Janeiro n zaocj u ouo bck"      
# [5] "no city present in this sentence"      
> unlist(sapply(seq(cities), function(i){
      grep(cities[i], tweets, value = TRUE)
  }))
# [1] "Niteroi rjzzfbymh wj fa elxbmyfk logeb"
# [2] "Rio de Janeiro n zaocj u ouo bck"      
# [3] "Vitória zl qxllds buoo fvclizxv zqf"   

```
Well, you don't need to iterate over both. grep can only take one pattern at a time, but can search for results in a vector just fine. So 

```
for city in cities
    grep(city, tweets)

```

is a bit more reasonable.