Error in data.frame(ID = names(result), distance = result): arguments imply differing number of rows: 0, 3

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)

You are applying a filter too early if you subsequent want to construct a dataframe by matching paired entries.
So filter after you make the dataframe

didnot get your point

Here is a solution for you.

library(geodist)
library(dplyr)
library(tidyr)
 
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$time=c(24,24,48,48,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

# make it a dataframe with names 
(result <- as.data.frame(distTable) %>% setNames(df$ID))

#Get all locations within a 5000m radius from one location
(result2 <- pivot_longer(result,cols = everything(),
                        names_to = "ID",
                        values_to = "distance") %>%
    filter(distance<5000))

(result3 <-  left_join(result2,df, by = "ID") %>% arrange(distance) )
  
# A tibble: 3 x 5
  ID    distance   lat   lon  time
  <chr>    <dbl> <dbl> <dbl> <dbl>
1 ID5      2816.  33.7  73.1    72
2 ID2      2943.  33.7  73.1    24
3 ID1      4655.  33.7  73.1    24

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.