Below is the sample data. Trying to add a new column of data d202002 to the "old" data frame but only doing so when they match (naics 123400,123500,123700) with 123600 having values in the d202001 column but not d202002. Kind of going in circles trying to merge these two.
library(dplyr)
library(data.table)
naics <- c(123400,123500,123600,123700,123400,123500,123600,123700)
area <- c(000001,000001,000001,000001,000003,000003,000003,000003)
areatype <- c(04,04,04,04,04,04,04,04)
state <- c(32,32,32,32,32,32,32,32)
d202001 <- c(16,18,20,22,24,26,28,30)
old <- data.frame(naics,area,areatype,state,d202001)
naicsnew <- c(123400,123500,123700,123800,123900,123400,123500,123700,123800,123900)
areanew <- c(000001,000001,000001,000001,000001,000003,000003,000003,000003,000003)
areatypenew <- c(04,04,04,04,04,04,04,04,04,04)
statenew <- c(32,32,32,32,32,32,32,32,32,32)
d202002 <- c(17,19,21,23,41,35,36,37,38,45)
new <- data.frame(naicsnew,areanew,areatypenew,statenew,d202002)
First attempt
new2 <- left_join(old,new, by = c("naics" = "naicsnew", "area" = "areanew", "areatype"="areatypenew","state"="statenew"))
Below is the intended result
naics area areatype state d202001 d202002
123400 1 4 32 16 17
123500 1 4 32 18 19
123700 1 4 32 22 21
123400 3 4 32 24 35
123500 3 4 32 26 36
123700 3 4 32 30 38