Loop over a data.table rows with condition

前端 未结 1 1043
天涯浪人
天涯浪人 2021-01-22 03:06

I have a data.table that holds ids and locations. for example, here is it with one row in it: (it has col and row names, don\'t know if it matters)

locations<-         


        
相关标签:
1条回答
  • No loops are required, just use data.table as intended. If all you want to see are the rows that within 50 meters from the desired location, all you have to do is

    locations[, if (gdist(-159.58, 21.901, location_lon, location_lat, units="m") <= 50) .SD, id]
    ##    id location_lon location_lat
    ## 1: 11      -159.58       21.901
    

    Here we are iterating by the id column within the locations data set itself and checking if each id is within 50 meters from -159.58, 21.901. If so, we are calling .SD which is basically the data set itself for that specific id.


    As a side note, data.table doesn't have row.names, so there is no need of specifiying them, see here, for example

    0 讨论(0)
提交回复
热议问题