dplyr left join keep y columns code example

Example: dplyr left join only certain columns

df1 <- data.frame(id=1:3,day=2:4,element=3:5,value1=100:102)
df2 <- data.frame(id=1:3,day=3:5,element=4:6,value2=200:202)
df1

#  id day element value1
#1  1   2       3    100
#2  2   3       4    101
#3  3   4       5    102

df2
#  id day element value2
#1  1   3       4    200
#2  2   4       5    201
#3  3   5       6    202

left_join(df1, df2)
#Joining by: c("id", "day", "element")
#  id day element value1 value2
#1  1   2       3    100     NA
#2  2   3       4    101     NA
#3  3   4       5    102     NA

left_join(df1, select(df2, c(id,value2)), by = "id")
#  id day element value1 value2
#1  1   2       3    100    200
#2  2   3       4    101    201
#3  3   4       5    102    202

Tags:

Misc Example