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

后端 未结 5 788
陌清茗
陌清茗 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条回答
  •  旧时难觅i
    2021-01-01 05:42

    Your data, changing stringsAsFactors=F

    DF1 <- data.frame("col1" = rep(c("A","B"), 18),
                  "col2" = rep(c("C","D","E"), 12),
                  "value"= (sample(1:100,36)),
                  "col4" = rep(NA,36),
                  stringsAsFactors=F)
    
    DF2 <- data.frame("col1" = rep("A",6),
                  "col2" = rep(c("C","D"),3),
                  "data" = rep(c(1,3),3),
                  "min" = seq(0,59,by=10),
                  "max" = seq(10,69,by=10),
                  stringsAsFactors=F)
    

    Using dplyr, 1) merge the two data using left_join, 2) check ifelse value is between min and max rowwise, then 3) unselect min and max columns...

    library(dplyr)
    left_join(DF1, DF2, by=c("col1","col2")) %>%
      rowwise() %>%
      mutate(data = ifelse(between(value,min,max), data, NA)) %>%
      select(-min, -max)
    

    Not sure if you were expecting to perform some kind of aggregation, but here's the output of the above code

        col1  col2 value  col4  data
     1     A     C    23    NA    NA
     2     A     C    23    NA     1
     3     A     C    23    NA    NA
     4     B     D    59    NA    NA
     5     A     E    57    NA    NA
     6     B     C     8    NA    NA
    

提交回复
热议问题