please help why this error is ocuring and rectify
library(geodist)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
#Set seed for reproducibility of random data generation
set.seed(72)
df1=c(lat=runif(1,33.45,33.75),lon=runif(1,72.83,73.17))
df1
#> lat lon
#> 33.72442 73.10897
df<-structure(
list(ID=paste0("ID", 1:5),
lat=runif(5,33.45,33.75),
lon=runif(5,72.83,73.17)),
Time=paste0("Time",c(24,24,48,48,72)),
col.Names = c("ID","lat", "lon","Time"),
row.names = c(NA, -5L),
class = c("data.table","data.frame"))
df
#> ID lat lon
#> 1 ID1 33.71310 73.06058
#> 2 ID2 33.71217 73.08081
#> 3 ID3 33.60412 73.04575
#> 4 ID4 33.68859 73.04256
#> 5 ID5 33.70490 73.08963
df$time=c(24,24,48,48,72)
head(df)
#> ID lat lon time
#> 1 ID1 33.71310 73.06058 24
#> 2 ID2 33.71217 73.08081 24
#> 3 ID3 33.60412 73.04575 48
#> 4 ID4 33.68859 73.04256 48
#> 5 ID5 33.70490 73.08963 72
df
#> ID lat lon time
#> 1 ID1 33.71310 73.06058 24
#> 2 ID2 33.71217 73.08081 24
#> 3 ID3 33.60412 73.04575 48
#> 4 ID4 33.68859 73.04256 48
#> 5 ID5 33.70490 73.08963 72
#Generate the distance table using haversine distance
distTable = geodist(df1,df, measure = "haversine")
distTable
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 4654.788 2942.625 14617.09 7330.103 2816.29
#Get all locations within a 5000m radius from one location
result = distTable[distTable[,] <= 5000]
result
#> [1] 4654.788 2942.625 2816.290
#Convert the result to a data frame and add the lat/lon
result = data.frame(ID = names(result), distance = result) %>%
left_join(df, by = "ID") %>% arrange(distance)
#> Error in data.frame(ID = names(result), distance = result): arguments imply differing number of rows: 0, 3
result
#> [1] 4654.788 2942.625 2816.290
Created on 2020-10-25 by the reprex package (v0.3.0)