data.frame(...,check.names = FALSE)中的错误:循环 [英] Error in data.frame(..., check.names = FALSE) : Looping For
问题描述
我正在处理大数据.我有2个数据集,分别为起源"和目的地".使用谷歌API,我想计算从ORIGIN数据集到DESTINATION数据集的方向.数据如下:
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)
}
}
执行后,我发现错误:
Error in data.frame(..., check.names = FALSE) :
argument is missing, with no default
该错误有解决方案吗?谢谢
Any solution for that error? Thank you
推荐答案
如果没有google-api,则可以使用 geosphere
包在本地完成:
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
在 dists
中,每一行对应于 masterdata
的行;每列对应于 xystation3375
的每一行.如果您想使用两个 No
列将其转换为更长的表,则可以使用
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
其中 Var1
是 masterdata $ No
.
这篇关于data.frame(...,check.names = FALSE)中的错误:循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!