Error in data.frame(…, check.names = FALSE) : Looping For

删除回忆录丶 提交于 2021-01-07 02:37:38

问题


I am working with large data. I have 2 datasets as ORIGIN and DESTINATION. Using google API, I would like to compute the direction from ORIGIN dataset to DESTINATION dataset. The data is as follows:

Origin(masterdata):
No xcoord  ycoord
1 109.6663 -6.897970
2 109.6584 -6.897511
3 109.6519 -6.893822
4 109.6586 -6.896936
5 109.6651 -6.897484

Destination(xystation3375)
No Long    Lat
1 109.6644 -6.889696
2 109.7008 -6.902980


key = "Google API
dataset_origin <- as.data.frame(masterdata[1:500,])
dataset_dest <- as.data.frame(xystation3375)

n = nrow(dataset_origin)

dest <- dataset_dest
k = nrow(dest)

data_od <- NULL
for(j in 1:k){
  for(i in 1:n){
    doc = mp_directions(
      origin = as.double(c(dataset_origin$xcoord[i],dataset_origin$ycoord[i])), #origin_ke-i. looping dari 1-20
      destination = as.double(c(dest$lng[j],dest$lat[j])), #destination ke_j; looping dari 1-3
      alternatives = TRUE,
      key = key,
      quiet = TRUE
    )

route = as.data.frame(mp_get_routes(doc))
route2 = cbind(route[1:7],
               rep(dataset_origin$D_NOP[i],nrow(route)),
               rep(dataset_origin$xcoord[i],nrow(route)),
               rep(dataset_origin$ycoord[i],nrow(route)),
               rep(dest$`place_name(xystation3375)`[j],nrow(route))
               )
data_od <- rbind.data.frame(data_od,route2,make.row.names = FALSE)


 }
}

After execution, I found error:

Error in data.frame(..., check.names = FALSE) : 
  argument is missing, with no default

Any solution for that error? Thank you


回答1:


Without google-api, this can be done locally with the geosphere package:

dists <- sapply(seq_len(nrow(xystation3375)), function(rn) {
  geosphere::distHaversine(masterdata[,c("xcoord","ycoord")], xystation3375[rn,c("Long","Lat")])
})

dists
#           [,1]     [,2]
# [1,]  944.6891 3853.277
# [2,] 1093.8555 4725.144
# [3,] 1455.7938 5499.434
# [4,] 1029.7686 4711.942
# [5,]  870.4009 3992.477

In dists, each row corresponds to the row of masterdata; each column corresponds to each row of xystation3375. If you want to convert this into a longer table using the two No columns, you can use

dimnames(dists) <- list(masterdata$No, xystation3375$No)
as.data.frame.table(dists)
#    Var1 Var2      Freq
# 1     1    1  944.6891
# 2     2    1 1093.8555
# 3     3    1 1455.7938
# 4     4    1 1029.7686
# 5     5    1  870.4009
# 6     1    2 3853.2772
# 7     2    2 4725.1437
# 8     3    2 5499.4340
# 9     4    2 4711.9420
# 10    5    2 3992.4767

where Var1 is masterdata$No.



来源:https://stackoverflow.com/questions/65182129/error-in-data-frame-check-names-false-looping-for

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!