I am trying to join 2 SpatialPointsDataFrames
by nearest neighbour analysis using sf::st_join()
. Both files have been converted using st_as_sf()
but when I try the join I get the error
Error in rep(seq_len(nrow(x)), lengths(i)) : invalid 'times' argument
At this point I have tried swapping the x and y arguments, and adjusting countless variations of the arguments but nothing seems to work. I have checked the help file for sf::st_join()
, but don't see anything about a times
argument? So I am unsure where from and why it keeps throwing this error...
below is a sample of my data set which produces the same error using the code further down
> head(sf.eSPDF[[1]])
Simple feature collection with 6 features and 8 fields
geometry type: POINT
dimension: XY
bbox: xmin: 35.9699 ymin: -3.74514 xmax: 35.97065 ymax: -3.74474
epsg (SRID): 4326
proj4string: +proj=longlat +datum=WGS84 +no_defs
# A tibble: 6 x 9
TIME ELEVATION LATITUDE LONGITUDE DATE V1 V2 Survey geometry
<dttm> <chr> <dbl> <dbl> <date> <dttm> <dttm> <dbl> <POINT [°]>
1 2012-01-20 07:26:05 1018 m -3.74 36.0 2012-01-20 2012-01-20 00:00:00 2012-01-31 00:00:00 1 (35.97047 -3.74474)
2 2012-01-20 07:27:35 1018 m -3.74 36.0 2012-01-20 2012-01-20 00:00:00 2012-01-31 00:00:00 1 (35.97057 -3.74486)
3 2012-01-20 07:27:39 1019 m -3.74 36.0 2012-01-20 2012-01-20 00:00:00 2012-01-31 00:00:00 1 (35.9706 -3.74489)
4 2012-01-20 07:27:47 1020 m -3.74 36.0 2012-01-20 2012-01-20 00:00:00 2012-01-31 00:00:00 1 (35.97065 -3.74489)
5 2012-01-20 07:28:05 1020 m -3.74 36.0 2012-01-20 2012-01-20 00:00:00 2012-01-31 00:00:00 1 (35.97035 -3.74498)
6 2012-01-20 07:28:26 1019 m -3.75 36.0 2012-01-20 2012-01-20 00:00:00 2012-01-31 00:00:00 1 (35.9699 -3.74514)
> head(sf.plt.centr)
Simple feature collection with 6 features and 1 field
geometry type: POINT
dimension: XY
bbox: xmin: 35.75955 ymin: -3.91594 xmax: 36.0933 ymax: -3.401
epsg (SRID): 4326
proj4string: +proj=longlat +datum=WGS84 +no_defs
PairID geometry
1 1 POINT (36.0933 -3.6731)
42 92 POINT (36.02593 -3.91594)
83 215 POINT (36.06496 -3.75837)
124 225 POINT (35.83156 -3.401)
165 251 POINT (35.75955 -3.54388)
206 2 POINT (36.08752 -3.69128)
Below is the code that I am using to check for working solutions
sf.eSPDF<-lapply(eSPDF, function(x){
st_as_sf(as(x, "SpatialPointsDataFrame"))
})
sf.plt.centr<-st_as_sf(as(plt.centr, "SpatialPointsDataFrame"))
x1<-head(sf.eSPDF[[1]])
x2<-head(sf.plt.centr)
check<-st_join(x1, x2, join=st_nn, maxdist = Inf, returnDist = T, progress = TRUE)
As you can see, the file I want to join to is an object within a list. All objects within that list have identical structure to the example given. Eventually I want to get a code that joins the sf.plt.centr
file to each of the files within the list. Something like...
big.join<-lapply(sf.eSPDF, function(x){
st_join('['(x), sf.plt.centr, st_nn, maxdist = Inf, returnDist = T, progress = TRUE)
}
x1<-head(sf.eSPDF[[1]]) %>% tibble() %>% mutate(geometry = geometry %>% st_sfc(crs=4326) %>% st_sf(crs=4326)) x2<-head(sf.plt.centr) %>% tibble() %>% mutate(geometry = geometry %>% st_sfc(crs=4326) %>% st_sf(crs=4326)) check<-st_join(x1, x2, join=st_nn, maxdist = Inf, returnDist = T, progress = TRUE)
– Osteoplastic