【问题标题】:How do I sort points based on a time matrix in R?如何根据 R 中的时间矩阵对点进行排序?
【发布时间】:2021-08-19 01:22:15
【问题描述】:

我在 R 中有一个时间矩阵,我在 osrm 包的帮助下计算了它。我想根据相邻点对点进行排序。 样本数据:

name <- LETTERS[1:10]
lat  <- c(22.57, 22.69, 22.72, 22.50, 22.66, 22.19, 22.60, 22.27, 22.31, 22.15)
lon  <- c(88.69, 88.84, 88.77, 88.85, 88.63, 88.91, 88.54, 88.62, 88.78, 88.66)
demand <- c(30, 70, 75, 100, 45, 60, 135, 65, 55, 50)

df<-data.frame(name, lon, lat, demand)

计算时间矩阵

library(osrm)


time<- osrmTable(df[,c('name', 'lon', 'lat')])
time_matrix<- time$durations

现在,我想要一个类似这样的数据框,基于上面的时间矩阵。

From To Time Demand
A    G  30.1 135
G    E  33.9 45
E    C  30.3 75

我可以找到最近的点,但我需要检查最近的点是否包含在 From 列中。如果是,则将使用第二个最近的点,依此类推。就像这里 G 的最近点是 A,但由于它已经包含在内,所以它将是 E(第二个最近点)。同样,它会一直持续到所有点都包含在表格中。

我该怎么做?

【问题讨论】:

    标签: r sorting geolocation geospatial osrm


    【解决方案1】:

    解决方案取决于起点(我们可以假设它是数据中的第一个点)以及如何选择后续点

    连续路径

    以下点是最近邻:

    diag(time_matrix) <- NA
    
    nearestpoints <- data.frame(matrix(ncol = 4, nrow = 0))
    colnames(nearestpoints) <- c("From", "To", "Time", "Demand")
    
    inputrowindex=1
    outputrowindex=1
    visitedpoints <- c(rownames(time_matrix)[1]) #The visited points are the 'To' points
    
    while(length(setdiff(rownames(time_matrix), visitedpoints)) > 0){
      nearest <- which.min(time_matrix[inputrowindex,])
      if(length(nearest)==0) break
      
      nearestpoints[outputrowindex, 1] <- rownames(time_matrix)[inputrowindex]
      nearestpoints[outputrowindex, 2] <- names(nearest)
      nearestpoints[outputrowindex, 3] <- time_matrix[inputrowindex, nearest]
      nearestpoints[outputrowindex, 4] <- df[nearest, 4]
      
      time_matrix[inputrowindex,] <- NA
      time_matrix[,inputrowindex] <- NA
      
      visitedpoints <- c(visitedpoints, names(nearest))
      
      inputrowindex = as.numeric(nearest) #Next point is the nearest
      outputrowindex = outputrowindex + 1
    }
    

    这给出了:

    head(nearestpoints)
    #  From To Time Demand
    #1    A  G 30.1    135
    #2    G  E 33.7     45
    #3    E  C 30.3     75
    #4    C  B 11.4     70
    #5    B  D 35.2    100
    #6    D  I 56.5     55
    

    数据排序路径

    以下是数据中的下一点:

    diag(time_matrix) <- NA
    
    nearestpoints <- data.frame(matrix(ncol = 4, nrow = 0))
    colnames(nearestpoints) <- c("From", "To", "Time", "Demand")
    
    inputrowindex=1
    outputrowindex=1
    
    visitedpoints <- c() #The visited points are the 'From' points
    
    while(length(setdiff(rownames(time_matrix), visitedpoints)) > 0){
      nearest <- which.min(time_matrix[inputrowindex,])
      if(length(nearest)==0) break
      
      nearestpoints[outputrowindex, 1] <- rownames(time_matrix)[inputrowindex]
      nearestpoints[outputrowindex, 2] <- names(nearest)
      nearestpoints[outputrowindex, 3] <- time_matrix[inputrowindex, nearest]
      nearestpoints[outputrowindex, 4] <- df[nearest, 4]
      
      time_matrix[inputrowindex,] <- NA
      time_matrix[,inputrowindex] <- NA
      
      visitedpoints <- c(visitedpoints,  rownames(time_matrix)[inputrowindex])
      
      inputrowindex = inputrowindex + 1 #Next point in the data
      outputrowindex = outputrowindex + 1
    }
    

    这给出了:

    head(nearestpoints)
    #  From To  Time Demand
    #1    A  G  30.1    135
    #2    B  C  11.4     75
    #3    C  E  30.3     45
    #4    D  I  56.5     55
    #5    E  G  33.9    135
    #6    F  H 118.1     65
    

    原始数据:

    name <- LETTERS[1:10]
    lat  <- c(22.57, 22.69, 22.72, 22.50, 22.66, 22.19, 22.60, 22.27, 22.31, 22.15)
    lon  <- c(88.69, 88.84, 88.77, 88.85, 88.63, 88.91, 88.54, 88.62, 88.78, 88.66)
    demand <- c(30, 70, 75, 100, 45, 60, 135, 65, 55, 50)
    df <- data.frame(name, lat, lon, demand)
    
    library(osrm)
    time <- osrmTable(df[,c('name', 'lon', 'lat')])
    time_matrix <- time$durations
    

    【讨论】:

      猜你喜欢
      • 2018-04-09
      • 2020-11-04
      • 2012-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多