R: merge based on multiple conditions (with non-equal criteria)

后端 未结 5 784
陌清茗
陌清茗 2021-01-01 05:26

I would like to merge 2 data frames based on multiple conditions.

DF1 <- data.frame(\"col1\" = rep(c(\"A\",\"B\"), 18),
                  \"col2\" = rep(c         


        
5条回答
  •  温柔的废话
    2021-01-01 05:59

    By using all.x=TRUE all rows of DF1 are kept then adjust condition in filter as follows:

    iMed=merge(DF1,DF2,by.x=c('col1','col2'),by.y=c('col1','col2'),all.x=TRUE)
    Res=iMed[is.na(iMed[,'min'])|is.na(iMed[,'max'])|(iMed[,'value']<=iMed[,'max'] & iMed[,'value']>=iMed[,'min'] ),]
    

提交回复
热议问题