How to Perform a VLOOKUP (Similar to Excel) in R?
Last Updated :
21 Feb, 2022
VLOOKUP is a function in excel and it is an acronym for vertical lookup. The task of this function is to search for a particular value in a column to return a value from a different column but in the same row.
Syntax:
VLOOKUP([value], [range], [column no], [true/false])
Here,
- value: Specifies the value to be searched
- range: It specifies the range in which the value has to be searched
- column no: The number of the column that contains the return value
- true: If the user wants approximate match
- false: If the user wants the exact match with the specified value
Method 1: Using Base R To Perform VLOOKUP:
We can achieve VLOOKUP in base R using the merge() function.
Syntax:
merge(dataFrame1, dataFrame2, by = "columnName")
Here,
- dataFrame1 and dataFrame2 are the dataFrames and by argument is optional and used to specify multiple columns to merge
Example:
In this program, firstly, we have created two dataframes. Then we have applied the merge function. Note that we have merged columns on the basis of section column which is the same in both the dataframes.
R
# R program to perform VLOOKUP
# using merge function
# creating a dataframe
dataFrame1 < - data.frame(section=LETTERS[1:15],
team=rep(c('Alpha', 'Beta', 'Gamma'),
each=5))
# creating another dataframe
dataFrame2 < - data.frame(section=LETTERS[1:15],
score=c(25, 13, 12, 16, 18, 19,
26, 28, 20, 36, 44, 29,
8, 6, 5))
# merge the two dataframes
merge(dataFrame1, dataFrame2, by="section")
Output:

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.
Syntax:
inner_join(dataFrame1, dataFrame2, by="columnName")
Here,
- dataFrame1 and dataFrame2 are the dataFrames and by argument is optional and used to specify multiple columns to merge
Syntax to install and import the dplyr package:
install.package('dplyr')
library(dplyr)
Example:
In this program, firstly, we have created two dataframes. Then we have applied the inner_join function. Note that we have merged columns on the basis of section column which is the same in both the dataframes.
R
# R program to perform VLOOKUP
# using dplyr
# Including library
library(dplyr)
# creating a dataframe
dataFrame1 <- data.frame(section=LETTERS[1:15],
team=rep(c('Alpha', 'Beta', 'Gamma'),
each=5))
# creating another dataframe
dataFrame2 <- data.frame(section=LETTERS[1:15],
score=c(25, 13, 12, 16, 18, 19,
26, 28, 20, 36, 44, 29,
8, 6, 5))
# merging the two dataframes by using
# inner_join function
inner_join(dataFrame1, dataFrame2, by="section")
Output:
Similar Reads
How to VLOOKUP to Return Multiple Values in One Cell in Excel? Almost everyone works with lots of data ranging from medium to high volume datasets, and extracting information from such a sheet can become easier, with little to advanced knowledge of excel. Excel is widely used to perform simple to complex data analysis. It is a great tool for generating meaningf
3 min read
How to VLOOKUP to Return Blank or Specific Value Instead of 0 or N/A in Excel? Excel is widely used for data management, and practically everyone utilizes it these days. At the same time, it is extremely user-friendly. Excel offers a number of functions and formulae that may make boring tasks much easier. VLOOKUP() is another extremely helpful method that is commonly used to '
6 min read
Different Ways to Perform a Case-Sensitive Vlookup in Excel In Excel datasets, sometimes you must have noticed, that there are many values in a table that are case-sensitive in nature. And if we want a particular type of value from our dataset, but unfortunately that value also has some duplicates, but with different case sensitivity, so here we need to sear
8 min read
How to plot excel data in R? Plotting graph in R using an excel file, we need an excel file with two-column in it, the values in the first column will be considered as the points at the x-axis and the values in the second column will be considered as the points at the y-axis. In this article, we will be discussing the approach
2 min read
How to Return Multiple Matching Rows and Columns Using VLOOKUP in Excel? VLOOKUP function is a premade (already made by Ms-Excel) function in Excel by which we can search for any information in a given spreadsheet. We can use the VLOOKUP function in two ways, first is VLOOKUP with an exact match and VLOOKUP with an approximate match. VLOOKUP with exact match means that k
4 min read