I have 2 data.frames
df <- data.frame(addr=c('a','b','c','d'), num = c(1,2,3,4))
> df
addr num
1 a 1
2 b 2
3 c 3
4 d 4
df2 <- data.frame(addr=c('a','b','d'), num=c(100,200,500))
> df2
addr num
1 a 100
2 b 200
3 d 500
And I would like to replace the values in df$num with the values from df2$num if the condition df$addr matches df2$addr. I managed to get this with the following code
df[,"num"] <- sapply(df[,"addr"], function(x) ifelse(x %in% df2$addr,df2$num[match(x,df2$addr)],df[df$addr==x,]$num))
> df
addr num
1 a 100
2 b 200
3 c 3
4 d 500
I was wondering if there is a more elegant method using dplyr or data.table?