R dplyr join by range or virtual column

后端 未结 6 1330
独厮守ぢ
独厮守ぢ 2020-12-10 16:33

I want to join two tibbles by a range or a virtual column. but it seems the by - parameter just allow to handle chr oder vector(chr) o

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-10 17:06

    I really liked @WiWeber's range_join function, but it gives an error if a record is not within range. Here's a modification

    library(dplyr)
    
    d <- tibble(value = c(seq(1,4, by = 0.2),9))
    r <- tibble(from = seq(1,5), to = c(seq(2,5),8), class = LETTERS[seq(1,5)])
    
    
    range_join <- function(x, y, value, left, right){
    all_matches <- tibble()
    x = as.data.frame(x)
    y = as.data.frame(y)
    x$index=x[,value]
    for (i in 1:nrow(y)){
        matches = x %>% filter(index>=y[i,left] & index<= y[i,right])
        if (nrow(matches)>0){
            all_matches = all_matches %>% bind_rows(matches %>% cbind(y[i,]))
        }
    }
    all_matches = all_matches %>% select(-index)
    return(all_matches)
    }
    
    
    data <- d %>% 
    range_join(r, "value", "from", "to")
    
    data
    

提交回复
热议问题