r - 解决旅行商的错误“较长的对象长度不是较短的对象长度的倍数”
问题描述
我正在学习 R 编程语言,我试图用它来计算我下次去葡萄牙旅行时将访问的一些地方之间的距离,但是我在从数据框中检索数据时遇到了麻烦。
我用这个字段建立了一个数据文件
- INITIAL_MONUMENT;
- FINAL_MONUMENT;
- 距离;
- INITIAL_CITY;
- 最终城市;
- 初始区域;
- 最终地区;
- INITIAL_DISTRICT;
- 决赛区;
其中前缀INITIAL_表示我从哪里来,前缀FINAL_表示我到达的地方。
为了简化微积分作为我的小项目的第一步,我只专注于访问首都(里斯本)。
我声明了一个包含里斯本所有纪念碑的向量,我想在纪念碑上使用 for 循环。
对于每个纪念碑,我使用min()函数来计算最近的纪念碑以字段DISTANCE
这是我的源代码:
#read my csv
dataPortugal <- "D:/workspaceR/prove/principiante/resources/viaggi/monuments-portugual.csv"
portugual = read.table(header=TRUE,sep=';',file = dataPortugal)
#names of columns: the instruction gives me the result I expect
colnames(portugual)
[1] "INITIAL_MONUMENT" "FINAL_MONUMENT" "DISTANCE" "INITIAL_CITY" "FINAL_CITY" "INITIAL_REGION" "FINAL_REGION"
[8] "INITIAL_DISTRICT" "FINAL_DISTRICT" "X"
#I concentrare only in monuments in Lisboa city: the instruction gives me the result I expect
only_lisbona <- portugual[portugual$INITIAL_CITY == "LISBONA" & portugual$FINAL_CITY == "LISBONA",]
#I delete the useless columns in case of my current single city: the instruction gives me the result I expect
only_lisbona <- subset(only_lisbona, select = c(1,2,3))
#I have a list of all possible monuments in Lisboa: the instruction gives me the result I expect
allMonuments <-(only_lisbona %>% distinct(INITIAL_MONUMENT))
myTrip <- #vector with the shortest trip from a monument to another
visitedPlaces <-#vector with the monuments I elaborated
#I consider every monument as departure to another monument
for(currentDeparture in allMonuments){
#I have a list of possible arrival Monuments
moreArrivals <- subset(only_lisbona, INITIAL_MONUMENT == currentDeparture)# ERROR
#after this point
#it's a draft of code
# it's not a part of my question
minimun <- min(moreArrivals)$DISTANCE
if(0 <minimun){
myTrip[currentDeparture] <- minimun
...#SET VISITED THE FINAL MONUMENT
for(currentDestination in allMonuments){
visitedPlaces<-subset(only_lisbona, INITIAL_MONUMENT == currentDeparture & FINAL_MONUMENT == currentDestination
& DISTANCE == minimun)
}
}else{
#TODO
}
}
我想循环每个纪念碑(循环)当前纪念碑是其他纪念碑的出发点实际下一个要参观的纪念碑是与实际出发纪念碑最小距离的纪念碑(我使用的是一个子集,由 INITIAL_MONUMENT 过滤等于当前离开)
但是,在我添加注释#ERROR 的那一行,控制台给了我这个错误信息
FUN(X[[i]], ...) 中的错误:
仅在具有所有数值变量的数据框上定义
另外:警告信息:
1:在
==.default
(INITIAL_MONUMENT,currentDeparture):较长的对象长度不是较短对象长度的倍数
2:在 is.na(e1) | is.na(e2) :
较长的对象长度不是较短对象长度的倍数
这个错误意味着我以错误的方式评估了我的目的地纪念碑moreArrivals 集,但是我可以通过哪些其他方式存储来自特定出发地的目的地子集?
注意:我认为这是一个类似于旅行商问题的问题:我有一个不同距离的景点列表,我会优化距离并每个地方触摸一次。
谢谢
先感谢您
解决方案
推荐阅读
- ios - 如何在 iOS 13 中识别用户最后一次交互的 UIScene
- python-3.x - 带有 conda 集成的 pySpark 抛出错误 pyspark 无法识别
- node.js - NodeJS / Express:发生不一致的[ERR_HTTP_HEADERS_SENT]
- java - 多次重启后如何唯一标识一个java进程
- python - 如何从熊猫数据框中的一列列表中的列表中随机选择
- android - 无法在 Android Studio 上重新打开组件树
- php - 未定义属性:stdClass::$Unblinking Eternity
- python - Python - 使用 scipy.stats.hypergeom 进行矢量化操作
- php - 如何修复 AWS Lightsail 服务器上的网关超时?
- r - 使用 testthat 检查数据框中的每个变量的 NA 值