簡體   English   中英

.pointsToMatrix(x)出錯:經度> 360,計算距離

[英]Error in .pointsToMatrix(x) : longitude > 360 with calculating distance

我試圖計算到點的最近距離。 因此我創建了兩個df

創建列表1

long <- c(52.070498, 52.370216, 52.632381, 53.201233, 51.813298, 51.92442, 52.266075, 53.219383, 52.960561, 52.367027, 51.571915, 52.516775, 51.441642, 52.221537, 52.090737, 51.985103, 51.560596)
lat <- c(4.3007, 4.895168,4.753375, 5.799913, 4.690093, 4.477733, 6.155217, 6.566502, 5.920522, 6.668492, 4.768323, 6.083022, 5.469722, 6.893662, 5.12142, 5.89873, 5.091914)

stad <- c("Den Haag", "Amsterdam", "Alkmaar", "Leeuwarden", "Dordrecht", "Rotterdam", "Deventer", "Groningen", "Heerenveen", "Almelo", "Breda", "Zwolle", "Eindhoven", "Enschede", "Utrecht", "Arnhem", "Tilburg")
list1 <- data.frame(longitude=long, latitude=lat, stad)

創建列表2

long <- c("51.476364", "52.12379", "52.456954", "51.543197", "52.307687", "53.250184", "52.640436", "53.397875", "52.491691", "52.109272", "52.121092", "52.882946", "52.518537", "53.201233", "52.711559", "52.066719", "53.405366", "52.433881", "53.128855", "51.641078", "53.189226", "52.716927", "51.453667", "51.233528", "51.529207", "51.980632", "51.429248", "51.92442", "51.441642", "50.851368")
lat <- c("6.180948", "4.438598", "4.606014", "6.087664", "4.767424", "4.951427", "4.998517", "5.346679", "4.593325", "5.180968", "5.285347", "5.360707", "5.471422", "5.799913", "5.864559", "5.894033", "6.212048", "6.232888", "6.587567", "5.619458", "7.162511", "5.737644", "3.570912", "3.830322", "3.89688", "4.134185", "4.304708", "4.477733", "5.469722", "5.690973")
stad <- c("Valkenburg", "Voorschoten", "Ijmuiden", "De kooy", "Schiphol", "Vlieland", "Berkhout", "Hoorn (terschelling)", "Wijk aan zee", "De Bilt", "Soesterberg", "Stavoren", "Lelystad", "Leeuwarden", "Marknesse", "Deelen", "Lauwersoog", "Heino", "Eelde", "Hupsel", "Nieuwe beerta", "Twente", "Vlissingen", "Westdorpe", "Wilhelminadorp", "Hoek van Holland", "Woensdrecht", "Rotterdam", "Eindhoven", "Maastricht")
list2 <- data.frame(longitude=long, latitude=lat , stad)

但是當我這樣做時:

 library(geosphere)
 mat <- distm(list1[,c('longitude','latitude')], list2[,c('longitude','latitude')], fun=distVincentyEllipsoid)

我明白了:

Error in .pointsToMatrix(x) : longitude > 360
list1$locality <- list2$stad[apply(mat, 1, which.min)]

顯然,問題是list2包含因子而不是longitudelatitude列中的浮點數。

一旦將因子值轉換為標簽指定的數值,它似乎有效:

list2$longitude.fix <- as.numeric(levels(list2$longitude))[list2$longitude]
list2$latitude.fix <- as.numeric(levels(list2$latitude))[list2$latitude]
distm(list1[,c('longitude','latitude')], list2[,c('longitude.fix','latitude.fix')], fun=distVincentyEllipsoid)
           [,1]      [,2]      [,3]      [,4]      [,5]      [,6]      [,7]      [,8]      …
 [1,] 218106.26  16356.00  54584.86 206073.80  57935.42 149371.56  99767.31 187239.60      …
 [2,] 173280.56  57416.26  33391.75 160587.51  15737.20  97796.65  32074.61 124403.87      …
 [3,] 203303.45  66306.56  25384.63 190634.04  36054.40  71943.23  27123.09 107285.19      …
 [4,] 195563.08 192179.83 155688.71 186323.20 151151.41  93985.73 108243.57  54651.40      …
 [5,] 169040.53  44278.35  72020.04 157421.51  55511.66 161989.79  97883.85 190131.21      …
 …

在你的第二個列表中你有字符串。

long <- as.numeric(c("51.476364", "52.12379", "52.456954", "51.543197", "52.307687", "53.250184", "52.640436", "53.397875", "52.491691", "52.109272", "52.121092", "52.882946", "52.518537", "53.201233", "52.711559", "52.066719", "53.405366", "52.433881", "53.128855", "51.641078", "53.189226", "52.716927", "51.453667", "51.233528", "51.529207", "51.980632", "51.429248", "51.92442", "51.441642", "50.851368"))
lat <- as.numeric(c("6.180948", "4.438598", "4.606014", "6.087664", "4.767424", "4.951427", "4.998517", "5.346679", "4.593325", "5.180968", "5.285347", "5.360707", "5.471422", "5.799913", "5.864559", "5.894033", "6.212048", "6.232888", "6.587567", "5.619458", "7.162511", "5.737644", "3.570912", "3.830322", "3.89688", "4.134185", "4.304708", "4.477733", "5.469722", "5.690973"))
stad <- c("Valkenburg", "Voorschoten", "Ijmuiden", "De kooy", "Schiphol", "Vlieland", "Berkhout", "Hoorn (terschelling)", "Wijk aan zee", "De Bilt", "Soesterberg", "Stavoren", "Lelystad", "Leeuwarden", "Marknesse", "Deelen", "Lauwersoog", "Heino", "Eelde", "Hupsel", "Nieuwe beerta", "Twente", "Vlissingen", "Westdorpe", "Wilhelminadorp", "Hoek van Holland", "Woensdrecht", "Rotterdam", "Eindhoven", "Maastricht")
list2 <- data.frame(longitude=long, latitude=lat , star)

或者你只是刪除引號

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM