I looked almost everywhere and cannot find the answer to this; R is the equivalent of VLOOKUP in Excel. VLOOKUP allows me to search for a specific value in a column and apply it to every row of my data frame.
In this case, I want to find the country in which a particular city is located (from the database), and return the name of the country to a new column.
So, I have this database:
countries <- c("UK", "US", "RUS")
cities <- c("LDN", "NY", "MOSC")
db <- cbind(countries, cities)
db
countries cities
[1,] "UK" "LDN"
[2,] "US" "NY"
[3,] "RUS" "MOSC"
And I want to find the country where these cities are located (replace NA), based on the above db:
df
countries cities
[1,] NA "LDN"
[2,] NA "NY"
[3,] NA "MOSC"
I do not know how to do this on R.
source
share