R: Check if value from dataframe is within range other dataframe

后端 未结 3 948
鱼传尺愫
鱼传尺愫 2021-01-18 05:43

I am looking for a way to look up infromation from 1 dataframe in another dataframe, get a value from that other dataframe and pass it back to the first frame..

exam

3条回答
  •  旧时难觅i
    2021-01-18 06:17

    Here's a simple base method that uses the OP's logic:

    f <- function(vec, id) {
      if(length(.x <- which(vec >= x$from & vec <= x$to & id == x$number))) .x else NA
    }
    y$name <- x$name[mapply(f, y$location, y$id_number)]
    y
    #  location id_number     name
    #1      1.5        30 region 1
    #2      2.8        30 region 2
    #3     10.0        38     
    #4      3.5        40     
    #5      2.0        36 region 7
    

提交回复
热议问题