1

我想确定一些地址的地理距离,并确定这些地址的平均值(平均距离)。

如果数据框只有一行,我找到了一个解决方案:

# 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  

不幸的是,我没有实现一个为整个数据集执行代码的函数。我目前的问题是,该函数不会按行计算距离平均值,而是计算数据集所有行的平均值。

#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)

你知道我犯了什么错误吗?

澄清我的问题:我正在尝试创建一个像这样的数据框(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

例如,每行距离的平均值。

4

1 回答 1

1

谢谢,我现在自己找到了解决方案:

# 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()
于 2019-09-23T13:23:53.097 回答