Merge 2 dataframes if value within range

后端 未结 4 1372
盖世英雄少女心
盖世英雄少女心 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

    I think the best approach is to change zones to a more friendly format for what you're doing:

    ZoneLookUp = lapply(split(zones, zones$Sentence), function(x) c(x$ZoneStart, x$ZoneEnd[nrow(x)]))
    
    #$`1`
    #[1] -8.86 -7.49 -5.88 -4.51 -2.90
    

    Then you can easily look up each zone:

    fixes$Zone = NULL
    for(i in 1:nrow(fixes))
        fixes$Zone[i] = cut(fixes$StartPosition[i], ZoneLookUp[[fixes$Sentence[i]]], labels=FALSE)
    

    If performance is an issue, you can take a (only) slightly less simple approach using by or data.table with by.

提交回复
热议问题