Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - Excel VLOOKUP equivalent - Lookup, replace [duplicate]

Tags:

r

vlookup

I have looked pretty much everywhere and cannot find the answer to this; R equivalent of VLOOKUP on Excel. VLOOKUP allows me to look up for a specific value throughout a column and apply it to each row of my data frame.

In this case I want to find the country a particular city is in (from a database) and return the name of the country in 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 want to find the country those cities are in (replace NA) based on the db above:

df
     countries cities
[1,]   NA      "LDN" 
[2,]   NA      "NY"  
[3,]   NA     "MOSC"

I have absolutely no idea how to go about this on R.

like image 227
JohnCoene Avatar asked Jun 03 '14 10:06

JohnCoene


People also ask

How do you do a VLOOKUP equivalent in R?

Method 2: Using dplyr To Perform VLOOKUP We can use the inner join function of the dplyr library in R to perform similar to the VLOOKUP function.

How do you do a VLOOKUP for duplicate lookup values?

The VLOOKUP Function always returns the first match. In order to return duplicate values (or the nth match) we need: A new unique identifier to differentiate all duplicate values. A helper column containing a list of unique IDs that will serve as the new lookup column (first column) of the table array.

Can VLOOKUP handle duplicates?

In our very first method, we'll use the VLOOKUP function to find duplicates. The VLOOKUP function can look up a value in the leftmost column of a data table and returns the corresponding value from another column that is located on the right side of the table.

What are the alternatives to VLOOKUP function in Excel?

Here we learn the top 4 alternatives to the VLOOKUP function including INDEX MATCH combination, LOOKUP function, XLOOKUP function and Filter function with examples and downloadable Excel template. You may also look at these useful functions –

How do I replicate the VLOOKUP function from excel in R?

We can replicate this function using base R or the dplyr package: The following examples show how to use each of these functions in R to replicate the VLOOKUP function from Excel. The following code shows how to perform a function similar to VLOOKUP in base R by using the merge () function:

How to find duplicates using VLOOKUP in MySQL?

The VLOOKUP function can look up a value in the leftmost column of a data table and returns the corresponding value from another column that is located on the right side of the table. Here, our lookup value will be from Column D and will find the duplicates from Column C. If a duplicate is found then it will show the state name.

What is the difference between VLOOKUP and match in SQL?

VLOOKUP can only work from left to right. MATCH function will return the row number. INDEX + MATCH and LOOKUP functions do not require column number, unlike VLOOKUP, require column number to fetch the data even though the required column is already selected.


2 Answers

You are performing a join which in R is performed using the function merge

merge(db, df)

Using the dplyr package allows more natural verbs:

library(dplyr)
inner_join(db, df)

or perhaps (if you want non-matches to be shown; see ?left_join for further information):

left_join(db, df)
like image 95
Hugh Avatar answered Oct 20 '22 01:10

Hugh


Here's another approach:

library(qdapTools)
df[, 1] <- df[, 2] %l% db[, 2:1]
like image 39
Tyler Rinker Avatar answered Oct 20 '22 01:10

Tyler Rinker