I have data like this:
library(data.table)
dtorig <- data.table(x=1:100,lat=(sample(c(800:900),100))/100, lon=(sample(c(3800:3900),100))/100)
I'd like to get the location of rows in dtorig that are farthest away from one another. My attempt (which doesn't work) is below:
###NOT WORK
library(geosphere)
extremep <- chull(dtorig[, c(3,2)])
extremepoints <- dtorig[extremep,]
wmax <- sapply(1:NROW(extremepoints), function(i) which.max(distm(extremepoints[i, c(3,2)],fun=distHaversine)))
Does this help?
library(data.table)
library(geosphere)
dtorig <- data.table(x=1:100,lat=(sample(c(800:900),100))/100, lon=(sample(c(3800:3900),100))/100)
x <- distm(x = as.matrix(dtorig[, .(lon, lat)]), fun=distHaversine)
which(x == max(x), arr.ind = TRUE)
# row col
# [1,] 50 27
# [2,] 27 50
Row 27 and 50 are the furthest apart.
Not using data.table
or geosphere
, but here is an sf
alternative that illustrates finding the right distances from the dense matrix given by st_distance
. Here you can see:
set.seed
so you should be able to reproduce this sf
points with st_as_sf
and an appropriate crs
by_element = FALSE
in st_distance
lower.tri
gather
up the rest of the matrix into a single distance column arrange(desc())
. plot
. library(tidyverse)
library(sf)
set.seed(12345)
dtorig <- tibble(
x = 1:100,
lat = (sample(c(800:900), 100)) / 100,
lon = (sample(c(3800:3900), 100)) / 100
)
sforig <- st_as_sf(dtorig, coords = c("lon", "lat"), crs = 4326)
distances <- sforig %>%
st_distance(by_element = FALSE) %>%
unclass %>%
`[<-`(lower.tri(., diag = TRUE), NA) %>%
as_tibble() %>%
rowid_to_column %>%
gather(colid, distance, starts_with("V"), na.rm = TRUE) %>%
arrange(desc(distance))
distances
#> # A tibble: 4,950 x 3
#> rowid colid distance
#> <int> <chr> <dbl>
#> 1 30 V68 138244.
#> 2 30 V75 137957.
#> 3 19 V30 135068.
#> 4 48 V78 131849.
#> 5 40 V90 131280.
#> 6 48 V90 130946.
#> 7 40 V78 130035.
#> 8 50 V68 128851.
#> 9 45 V48 128805.
#> 10 40 V45 128531.
#> # ... with 4,940 more rows
sforig %>%
st_geometry %>%
plot(col = "red", pch = 19)
sforig %>%
filter(x %in% c(30, 68)) %>%
st_geometry %>%
plot( add = TRUE, col = "blue", pch = 19)
Created on 2018-07-25 by the reprex package (v0.2.0).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.