How to do Excel VLOOKUP in R (using left_join, merge)

This tutorial helps you code Excel’s VLOOKUP (Exact Match) functionality in R using dplyr’s left_join() and Base-R’s merge(). Please let me know your feedback if this can help Excel users try out R and get confident about doing Data Analytics in R

Youtube - https://www.youtube.com/watch?v=GsxlOwa4dSg

Video Tutorial

Code

# library tidyverse for data manipulation and plot

library(tidyverse)

# reading input dataset

co2 <- read_csv("C:/users/abdrs/Downloads/food_consumption.csv")


countries <- read_csv("C:/users/abdrs/Downloads/Countries-Continents.csv")


countries$some_number <- 6

# method 1

co2 <- co2 %>% 
  left_join(countries, by = c('country' = 'Country')) 

# method 2

co2 <- merge(co2, countries,  
      by.x = 'country',
      by.y = "Country")
 
comments powered by Disqus