Merge 2 dataframes if value within range

后端 未结 4 1371
盖世英雄少女心
盖世英雄少女心 2020-11-27 22:13

I have been struggling with this for some time now and couldn\'t find any way of doing it, so I would be incredibly grateful if you could help! I am a novice in programming

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-27 22:42

    With version v1.9.8 (on CRAN 25 Nov 2016), data.table has gained the ability to perform non-equi joins and range joins:

    library(data.table)
    setDT(fixes)[setDT(zones), 
                 on = .(Sentence, StartPosition >= ZoneStart, StartPosition < ZoneEnd), 
                 Zone := Zone][]
    
       Order Participant Sentence Fixation StartPosition Zone
    1:     1           1        1        1         -6.89    2
    2:     2           1        1        2         -5.88    3
    3:     3           1        1        3         -5.33    3
    4:     4           1        1        4         -4.09    4
    5:     5           1        1        5         -5.36    3
    

    Data

    fixes <- readr::read_table(
      "Order Participant Sentence Fixation StartPosition
      1       1          1         1       -6.89
      2       1          1         2       -5.88
      3       1          1         3       -5.33
      4       1          1         4       -4.09
      5       1          1         5       -5.36"
    )
    zones <- readr::read_table(
      "Sentence     Zone  ZoneStart   ZoneEnd
      1           1     -8.86      -7.49
      1           2     -7.49      -5.89
      1           3     -5.88      -4.51
      1           4     -4.51      -2.90"
    )
    

提交回复
热议问题