I found that I did a “conditional left join” several times in R. To illustrate an example; if you have two data frames, for example:
> df ab 1 1 0 2 2 0 > other.df ab 1 2 3
The goal is to end this data frame:
> final.df ab 1 1 0 2 2 3
The code I have written so far:
c <- merge(df, other.df, by=c("a"), all.x = TRUE) c[is.na(c$by),]$by <- 0 d<-subset(c, select=c("a","by")) colnames(d)[2]<-b
to finally come up with the result that I wanted.
Doing this in four steps makes the code very opaque. Is there a better, less cumbersome way to do this?
r conditional left-join
svenski
source share