Loop over a data.table rows with condition
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
KeshetE
Updated on June 04, 2022Comments
-
KeshetE almost 2 years
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<-data.table(c(11,12),c(-159.58,0.2),c(21.901,22.221)) colnames(locations)<-c("id","location_lon","location_lat") rownames(locations)<-c("1","2")
I then want to iterate over the rows and compare them to another point (with lat,lon). In a for loop it works:
for (i in 1:nrow(locations)) { loc <- locations[i,] dist <- gdist(-159.5801, 21.901, loc$location_lon, loc$location_lat, units="m") if(dist <= 50) { return (loc) } return (NULL) }
and returns:
id location_lon location_lat
1: 11 -159.58 21.901
but I want to use apply. The following code fails to run:
dists <- apply(locations,1,function(x) if (50 - gdist(-159.5801, 21.901, x$location_lon, x$location_lat, units="m")>=0) x else NULL)
with
$ operator is invalid for atomic vectors
error. Changing to reference by location (x[2],x[3]
) isn't enough to fix this, I getError in if (radius - gdist(lon, lat, x[2], x[3], units = "m") >= 0) x else NULL : missing value where TRUE/FALSE needed
This is because the data.table is converted to matrix, and the coordinates are treated as text instead of numbers. Is there a way to overcome this? The solution needs to be efficient (I want to run this check for >1,000,000 different coordinates). Changing the data structure of the locations table is possible if needed.
-
David Arenburg over 9 yearsNP, I really advise you to read some
data.table
documentation, as it seems like you are not using it as intended. There are many good answers on SO on the data.table tag posted by @Arun that you can learn alot from