画一个圈,找出R中各站之间的距离

plicqrtu  于 2023-06-03  发布在  其他
关注(0)|答案(2)|浏览(197)

我已经做了这个代码来查找站之间的距离,但在输出中,只有一个值。你能找到错误吗?

df <- data.frame(
  station = rep(c("A", "B", "C", "D"), each = 20),
  temperature = rnorm(80),
  latitude = c(40.7128, 34.0522, 41.8781, 39.9526),
  longitude = c(-74.0060, -118.2437, -87.6298, -75.1652)
)
stations <- unique(df$station)

my_points <- matrix(NA, nrow = length(unique(df$station)), ncol = length(unique(df$station)))

# Loop through each station combination
for (i in 1:length(stations)) {
  for (j in 1:length(stations)) {
    # Get temperatures for the two stations
    lat1 <- df$latitude[df$station == stations[i]]
    lon1 <- df$longitude[df$station == stations[i]]
    lat2 <- df$latitude[df$station == stations[j]]
    lon2 <- df$longitude[df$station == stations[j]]
    my_points[i, j] <- as.vector(dist(matrix(c(lon1,lon2,lat1,lat2),
                                             nrow = 2)))  
    
  }
}

distance_df <- as.data.frame(my_points)
uubf1zoe

uubf1zoe1#

这里有两个问题:

  • 输入数据框的外观可能与您期望的不一样-纬度和经度列被循环使用,因此同一个测站有多个不同的坐标。尝试在lat和long列中添加rep()以及station
  • 在代码中,lat1 <- df$latitude[df$station == stations[i]]返回一个向量,因为有多个匹配项。我认为你期待的是一个单一的值。只使用第一个匹配元素(因为它们现在都是向量中的相同元素,在如上所述添加rep()之后):
df <- data.frame(
  station = rep(c("A", "B", "C", "D"), each = 20),
  temperature = rnorm(80),
  latitude = rep(c(40.7128, 34.0522, 41.8781, 39.9526), each = 20),
  longitude = rep(c(-74.0060, -118.2437, -87.6298, -75.1652), each = 20)
)
stations <- unique(df$station)

my_points <- matrix(NA, nrow = length(unique(df$station)), ncol = length(unique(df$station)))

# Loop through each station combination
for (i in 1:length(stations)) {
  for (j in 1:length(stations)) {
    # Get temperatures for the two stations
    lat1 <- df$latitude[df$station == stations[i]][1]
    lon1 <- df$longitude[df$station == stations[i]][1]
    lat2 <- df$latitude[df$station == stations[j]][1]
    lon2 <- df$longitude[df$station == stations[j]][1]
    my_points[i, j] <- as.vector(dist(matrix(c(lon1,lon2,lat1,lat2),
                                             nrow = 2)))  
    
  }
}

distance_df <- as.data.frame(my_points)

这给出:

V1       V2       V3        V4
1  0.000000 44.73631 13.67355  1.386235
2 44.736313  0.00000 31.59835 43.480707
3 13.673546 31.59835  0.00000 12.612446
4  1.386235 43.48071 12.61245  0.000000

一个稍微好一点的方法来找到独特的车站:

unique(df[, c("station", "latitude", "longitude")])

然后你可以循环遍历它们:

# Loop through each station combination
for (i in 1:length(stations)) {
  for (j in 1:length(stations)) {
    # Get temperatures for the two stations
    lat1 <- unique_df$latitude[unique_df$station == stations[i]]
    lon1 <- unique_df$longitude[unique_df$station == stations[i]]
    lat2 <- unique_df$latitude[unique_df$station == stations[j]]
    lon2 <- unique_df$longitude[unique_df$station == stations[j]]
    my_points[i, j] <- as.vector(dist(matrix(c(lon1,lon2,lat1,lat2),
                                             nrow = 2)))  
    
  }
}
7rtdyuoh

7rtdyuoh2#

不需要循环-只需要子集到唯一的站纬度和经度,并在其上使用dist()

# assuming each station has a unique location
stations <- unique(df[c("latitude", "longitude")])
rownames(stations) <- unique(df$station)

dist(stations) |> 
  as.matrix() |> 
  as.data.frame()
A        B        C         D
A  0.000000 44.73631 13.67355  1.386235
B 44.736313  0.00000 31.59835 43.480707
C 13.673546 31.59835  0.00000 12.612446
D  1.386235 43.48071 12.61245  0.000000
  • 示例数据:*
df <- data.frame(
  station = rep(c("A", "B", "C", "D"), each = 20),
  temperature = rnorm(80),
  latitude = rep(c(40.7128, 34.0522, 41.8781, 39.9526), each = 20),
  longitude = rep(c(-74.0060, -118.2437, -87.6298, -75.1652), each = 20)
)

相关问题