首页 > 解决方案 > 解决旅行商的错误“较长的对象长度不是较短的对象长度的倍数”

问题描述

我正在学习 R 编程语言,我试图用它来计算我下次去葡萄牙旅行时将访问的一些地方之间的距离,但是我在从数据框中检索数据时遇到了麻烦。

我用这个字段建立了一个数据文件

其中前缀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 集,但是我可以通过哪些其他方式存储来自特定出发地的目的地子集?

注意:我认为这是一个类似于旅行商问题的问题:我有一个不同距离的景点列表,我会优化距离并每个地方触摸一次。

谢谢

先感谢您

标签: rsubsetgraph-algorithm

解决方案


推荐阅读