简体   繁体   中英

Average distance between subsets of points with ggmap /geosphere

I would like to determine the geographical distances from a number of addresses and determine the mean value (the mean distance) from these.

In case the dataframe has only one row, I have found a solution:

# Pakete laden
library(readxl)
library(openxlsx)
library(googleway)
#library(sf)
library(tidyverse)
library(geosphere)
library("ggmap")

#API Key bestimmen
set_key("")
api_key <- ""
register_google(key=api_key)

#  Data
df <- data.frame(
  V1 = c("80538 München, Germany", "01328 Dresden, Germany", "80538 München, Germany",
         "07745 Jena, Germany",    "10117 Berlin, Germany"),
  V2 = c("82152 Planegg, Germany", "01069 Dresden, Germany", "82152 Planegg, Germany",
         "07743 Jena, Germany",    "14195 Berlin, Germany"),
  V3 = c("85748 Garching, Germany", "01069 Dresden, Germany",  "85748 Garching, Germany",
         NA,     "10318 Berlin, Germany"),
  V4 = c("80805 München, Germany", "01187 Dresden, Germany", "80805 München, Germany",
         "07745 Jena, Germany", NA), stringsAsFactors=FALSE
)

#replace NA for geocode-funktion
df[is.na(df)] <- ""

#slice it
df1 <- slice(df, 5:5)

#  lon lat Informations
df_2 <- geocode(c(df1$V1, df1$V2,df1$V3, df1$V4)) %>% na.omit()

# to Matrix
mat_df  <- as.matrix(df_2) 

#dist-mat
dist_mat <- distm(mat_df)

#mean-dist of row 5
mean(dist_mat[lower.tri(dist_mat)])/1000  

Unfortunately, I fail to implement a function that executes the code for an entire data set. My current problem is, that the function does not calculate the distance-averages rowwise, but calculates the average value from all lines of the data set.

#Funktion

Mean_Dist <- function(df,w,x,y,z) {

  # for (row in 1:nrow(df)) {
  #   dist_mat <- geocode(c(w, x, y, z))
  #   
  # }

  df <- geocode(c(w, x, y, z)) %>% na.omit() # ziehe lon lat Informationen aus Adressen

  mat_df <- as.matrix(df) # schreibe diese in eine Matrix

  dist_mat <- distm(mat_df)

  dist_mean <- mean(dist_mat[lower.tri(dist_mat)])

  return(dist_mean)
}

df %>%  mutate(lon =  Mean_Dist(df,df$V1, df$V2,df$V3, df$V4)/1000)

Do you have any idea what mistake I made?

to clarify my question: What I'm trying to create a dataframe like this one (V5):

  V1                     V2                     V3                      V4                      V5                    
  <chr>                  <chr>                  <chr>                   <chr>                   <numeric>                 
1 80538 München, Germany 82152 Planegg, Germany 85748 Garching, Germany 80805 München, Germany Mean_Dist_row1
2 01328 Dresden, Germany 01069 Dresden, Germany 01069 Dresden, Germany  01187 Dresden, Germany Mean_Dist_row2
3 80538 München, Germany 82152 Planegg, Germany 85748 Garching, Germany 80805 München, Germany Mean_Dist_row3
4 07745 Jena, Germany    07743 Jena, Germany    07745 Jena, Germany     07745 Jena, Germany Mean_Dist_row4   
5 10117 Berlin, Germany  14195 Berlin, Germany  10318 Berlin, Germany   14476 Potsdam, Germany Mean_Dist_row5

eg an average of the distance of each row.

thank you, I have now found a solution myself:

# NAs durch leeren String ersetzen für die geocode()-Funktion
df[is.na(df)] <- ""
# in Matrix umwandeln
mat_df <- as.matrix(df)
# lon lat Daten zeilenweise ziehen
list <- apply(mat_df,MAR=1,geocode)
# Dist-Daten-Matritzen für jedes Element in der Liste ziehen
list_dist <-lapply(list,distm) 

# Schreibe Funktion, die die untere Dreiecksmatrizen über die Hauptmatrix schreibt damit Distanzen bei Berechnung des Mittelwertes
# nicht doppelt gezählt werden
f <- function(m) {
    m <- (m)[lower.tri(m)]
    m
}

#wende diese Funktion auf alle Listenelemente an (Hauptmatritzen durch Dreiecksmatrizen)
list_dist <- lapply(list_dist,f) 

# Na´s aus List entfernen: 
list_dist <- lapply(list_dist, function(x) x[!is.na(x)])

# ziehe Mittelwerte und binde sie zurück anden Dataframe
df$mean_dist <- lapply(list_dist, mean) %>% as.numeric()

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM