使用 Sys.sleep() 每 2 分钟循环一次逐行计算距离
Loop to compute distances row by row every 2 minutes using Sys.sleep()
我有一个如下所示的数据框(我将 GP_loc
和 Hosp_loc
用作 "from" 和 "to"):
structure(list(Hosp = c("RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST"),
Hosplat = c(52.4532708, 52.4532708, 52.4532708, 52.4532708,
52.4532708, 52.4532708, 52.4532708, 52.4532708, 52.4532708,
52.4532708, 52.4532708), Hosplon = c(-1.936283476, -1.936283476,
-1.936283476, -1.936283476, -1.936283476, -1.936283476, -1.936283476,
-1.936283476, -1.936283476, -1.936283476, -1.936283476),
x = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), GP = c("A81004 - WOODLANDS ROAD SURGERY",
"A81017 - WOODBRIDGE PRACTICE", "A81021 - NORMANBY MEDICAL CENTRE",
"A81022 - HILLSIDE PRACTICE", "A81025 - THE DOVECOT SURGERY",
"A81031 - HAVELOCK GRANGE PRACTICE", "A81035 - NEWLANDS MEDICAL CENTRE",
"A81046 - WOODLANDS FAMILY MEDICAL CENTRE", "A81063 - THE HEADLAND MEDICAL CENTRE",
"A81064 - THE DISCOVERY PRACTICE", "A81618 - HUNTCLIFF SURGERY"
), GPlat = c(54.571614, 54.53808, 54.570665, 54.563124, 54.561689,
54.682334, 54.571908, 54.561397, 54.696642, 54.57537, 54.5828
), GPlon = c(-1.232274, -1.292145, -1.167559, -0.980768,
-1.318938, -1.214149, -1.226236, -1.321186, -1.183105, -1.236504,
-0.974691), Hosp_loc = c("52.45327 -1.936283", "52.45327 -1.936283",
"52.45327 -1.936283", "52.45327 -1.936283", "52.45327 -1.936283",
"52.45327 -1.936283", "52.45327 -1.936283", "52.45327 -1.936283",
"52.45327 -1.936283", "52.45327 -1.936283", "52.45327 -1.936283"
), GP_loc = c("54.571614 -1.232274", "54.53808 -1.292145",
"54.570665 -1.167559", "54.563124 -.980768", "54.561689 -1.318938",
"54.682334 -1.214149", "54.571908 -1.226236", "54.561397 -1.321186",
"54.696642 -1.183105", "54.57537 -1.236504", "54.5828 -.974691"
)), .Names = c("Hosp", "Hosplat", "Hosplon", "x", "GP", "GPlat",
"GPlon", "Hosp_loc", "GP_loc"), class = "data.frame", row.names = c(NA,
-11L))
我通常这样计算距离:
library(ggmap)
results <- mapdist(drivetime$GP_loc, drivetime$Hosp_loc, mode = "driving", override_limit = TRUE)
为了避免 Google 地图 API 100 queries/command 的限制,我想我可以创建一个循环 "Compute the distance from GP_loc to Hosp_loc, one at a time and every 2 minutes"。
有点像这样:
for (i in 1:10) {
results4 <- mapdist(drivetime$GP_loc[i], drivetime$Hosp_loc[i], mode = "driving", override_limit = TRUE)
Sys.sleep(10)
}
我是R新手,请多多包涵!
有什么建议么?谢谢!!
根据评论中的说明,这是一个可重现的答案。您可以循环并: i) 存储中间值
列表中的结果,ii) 稍等一下,然后 rbind 整个列表以获得友好的 data.frame
library(ggmap)
drivetime <- structure(list(Hosp = c("RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST"),
Hosplat = c(52.4532708, 52.4532708, 52.4532708, 52.4532708,
52.4532708, 52.4532708, 52.4532708, 52.4532708, 52.4532708,
52.4532708, 52.4532708), Hosplon = c(-1.936283476, -1.936283476,
-1.936283476, -1.936283476, -1.936283476, -1.936283476, -1.936283476,
-1.936283476, -1.936283476, -1.936283476, -1.936283476),
x = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), GP = c("A81004 - WOODLANDS ROAD SURGERY",
"A81017 - WOODBRIDGE PRACTICE", "A81021 - NORMANBY MEDICAL CENTRE",
"A81022 - HILLSIDE PRACTICE", "A81025 - THE DOVECOT SURGERY",
"A81031 - HAVELOCK GRANGE PRACTICE", "A81035 - NEWLANDS MEDICAL CENTRE",
"A81046 - WOODLANDS FAMILY MEDICAL CENTRE", "A81063 - THE HEADLAND MEDICAL CENTRE",
"A81064 - THE DISCOVERY PRACTICE", "A81618 - HUNTCLIFF SURGERY"
), GPlat = c(54.571614, 54.53808, 54.570665, 54.563124, 54.561689,
54.682334, 54.571908, 54.561397, 54.696642, 54.57537, 54.5828
), GPlon = c(-1.232274, -1.292145, -1.167559, -0.980768,
-1.318938, -1.214149, -1.226236, -1.321186, -1.183105, -1.236504,
-0.974691), Hosp_loc = c("52.45327 -1.936283", "52.45327 -1.936283",
"52.45327 -1.936283", "52.45327 -1.936283", "52.45327 -1.936283",
"52.45327 -1.936283", "52.45327 -1.936283", "52.45327 -1.936283",
"52.45327 -1.936283", "52.45327 -1.936283", "52.45327 -1.936283"
), GP_loc = c("54.571614 -1.232274", "54.53808 -1.292145",
"54.570665 -1.167559", "54.563124 -.980768", "54.561689 -1.318938",
"54.682334 -1.214149", "54.571908 -1.226236", "54.561397 -1.321186",
"54.696642 -1.183105", "54.57537 -1.236504", "54.5828 -.974691"
)), .Names = c("Hosp", "Hosplat", "Hosplon", "x", "GP", "GPlat",
"GPlon", "Hosp_loc", "GP_loc"), class = "data.frame", row.names = c(NA,
-11L))
这是循环:
res <- vector("list")
for (i in 1:10) { # could be 1:nrow(drivetime)
res[[i]] <- mapdist(drivetime$GP_loc[i], drivetime$Hosp_loc[i], mode = "driving", override_limit = TRUE)
Sys.sleep(10)
}
res_df <- do.call(rbind, res)
结果:
> res_df
from to m km miles seconds minutes hours
1 54.571614 -1.232274 52.45327 -1.936283 286142 286.142 177.8086 11061 184.3500 3.072500
2 54.53808 -1.292145 52.45327 -1.936283 281690 281.690 175.0422 10908 181.8000 3.030000
3 54.570665 -1.167559 52.45327 -1.936283 291233 291.233 180.9722 11244 187.4000 3.123333
4 54.563124 -.980768 52.45327 -1.936283 302302 302.302 187.8505 11748 195.8000 3.263333
5 54.561689 -1.318938 52.45327 -1.936283 287334 287.334 178.5493 11079 184.6500 3.077500
6 54.682334 -1.214149 52.45327 -1.936283 301669 301.669 187.4571 11843 197.3833 3.289722
7 54.571908 -1.226236 52.45327 -1.936283 286680 286.680 178.1430 10956 182.6000 3.043333
8 54.561397 -1.321186 52.45327 -1.936283 287460 287.460 178.6276 11109 185.1500 3.085833
9 54.696642 -1.183105 52.45327 -1.936283 309272 309.272 192.1816 12062 201.0333 3.350556
10 54.57537 -1.236504 52.45327 -1.936283 285644 285.644 177.4992 11001 183.3500 3.055833
我有一个如下所示的数据框(我将 GP_loc
和 Hosp_loc
用作 "from" 和 "to"):
structure(list(Hosp = c("RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST"),
Hosplat = c(52.4532708, 52.4532708, 52.4532708, 52.4532708,
52.4532708, 52.4532708, 52.4532708, 52.4532708, 52.4532708,
52.4532708, 52.4532708), Hosplon = c(-1.936283476, -1.936283476,
-1.936283476, -1.936283476, -1.936283476, -1.936283476, -1.936283476,
-1.936283476, -1.936283476, -1.936283476, -1.936283476),
x = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), GP = c("A81004 - WOODLANDS ROAD SURGERY",
"A81017 - WOODBRIDGE PRACTICE", "A81021 - NORMANBY MEDICAL CENTRE",
"A81022 - HILLSIDE PRACTICE", "A81025 - THE DOVECOT SURGERY",
"A81031 - HAVELOCK GRANGE PRACTICE", "A81035 - NEWLANDS MEDICAL CENTRE",
"A81046 - WOODLANDS FAMILY MEDICAL CENTRE", "A81063 - THE HEADLAND MEDICAL CENTRE",
"A81064 - THE DISCOVERY PRACTICE", "A81618 - HUNTCLIFF SURGERY"
), GPlat = c(54.571614, 54.53808, 54.570665, 54.563124, 54.561689,
54.682334, 54.571908, 54.561397, 54.696642, 54.57537, 54.5828
), GPlon = c(-1.232274, -1.292145, -1.167559, -0.980768,
-1.318938, -1.214149, -1.226236, -1.321186, -1.183105, -1.236504,
-0.974691), Hosp_loc = c("52.45327 -1.936283", "52.45327 -1.936283",
"52.45327 -1.936283", "52.45327 -1.936283", "52.45327 -1.936283",
"52.45327 -1.936283", "52.45327 -1.936283", "52.45327 -1.936283",
"52.45327 -1.936283", "52.45327 -1.936283", "52.45327 -1.936283"
), GP_loc = c("54.571614 -1.232274", "54.53808 -1.292145",
"54.570665 -1.167559", "54.563124 -.980768", "54.561689 -1.318938",
"54.682334 -1.214149", "54.571908 -1.226236", "54.561397 -1.321186",
"54.696642 -1.183105", "54.57537 -1.236504", "54.5828 -.974691"
)), .Names = c("Hosp", "Hosplat", "Hosplon", "x", "GP", "GPlat",
"GPlon", "Hosp_loc", "GP_loc"), class = "data.frame", row.names = c(NA,
-11L))
我通常这样计算距离:
library(ggmap)
results <- mapdist(drivetime$GP_loc, drivetime$Hosp_loc, mode = "driving", override_limit = TRUE)
为了避免 Google 地图 API 100 queries/command 的限制,我想我可以创建一个循环 "Compute the distance from GP_loc to Hosp_loc, one at a time and every 2 minutes"。
有点像这样:
for (i in 1:10) {
results4 <- mapdist(drivetime$GP_loc[i], drivetime$Hosp_loc[i], mode = "driving", override_limit = TRUE)
Sys.sleep(10)
}
我是R新手,请多多包涵! 有什么建议么?谢谢!!
根据评论中的说明,这是一个可重现的答案。您可以循环并: i) 存储中间值
列表中的结果,ii) 稍等一下,然后 rbind 整个列表以获得友好的 data.frame
library(ggmap)
drivetime <- structure(list(Hosp = c("RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST",
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST"),
Hosplat = c(52.4532708, 52.4532708, 52.4532708, 52.4532708,
52.4532708, 52.4532708, 52.4532708, 52.4532708, 52.4532708,
52.4532708, 52.4532708), Hosplon = c(-1.936283476, -1.936283476,
-1.936283476, -1.936283476, -1.936283476, -1.936283476, -1.936283476,
-1.936283476, -1.936283476, -1.936283476, -1.936283476),
x = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), GP = c("A81004 - WOODLANDS ROAD SURGERY",
"A81017 - WOODBRIDGE PRACTICE", "A81021 - NORMANBY MEDICAL CENTRE",
"A81022 - HILLSIDE PRACTICE", "A81025 - THE DOVECOT SURGERY",
"A81031 - HAVELOCK GRANGE PRACTICE", "A81035 - NEWLANDS MEDICAL CENTRE",
"A81046 - WOODLANDS FAMILY MEDICAL CENTRE", "A81063 - THE HEADLAND MEDICAL CENTRE",
"A81064 - THE DISCOVERY PRACTICE", "A81618 - HUNTCLIFF SURGERY"
), GPlat = c(54.571614, 54.53808, 54.570665, 54.563124, 54.561689,
54.682334, 54.571908, 54.561397, 54.696642, 54.57537, 54.5828
), GPlon = c(-1.232274, -1.292145, -1.167559, -0.980768,
-1.318938, -1.214149, -1.226236, -1.321186, -1.183105, -1.236504,
-0.974691), Hosp_loc = c("52.45327 -1.936283", "52.45327 -1.936283",
"52.45327 -1.936283", "52.45327 -1.936283", "52.45327 -1.936283",
"52.45327 -1.936283", "52.45327 -1.936283", "52.45327 -1.936283",
"52.45327 -1.936283", "52.45327 -1.936283", "52.45327 -1.936283"
), GP_loc = c("54.571614 -1.232274", "54.53808 -1.292145",
"54.570665 -1.167559", "54.563124 -.980768", "54.561689 -1.318938",
"54.682334 -1.214149", "54.571908 -1.226236", "54.561397 -1.321186",
"54.696642 -1.183105", "54.57537 -1.236504", "54.5828 -.974691"
)), .Names = c("Hosp", "Hosplat", "Hosplon", "x", "GP", "GPlat",
"GPlon", "Hosp_loc", "GP_loc"), class = "data.frame", row.names = c(NA,
-11L))
这是循环:
res <- vector("list")
for (i in 1:10) { # could be 1:nrow(drivetime)
res[[i]] <- mapdist(drivetime$GP_loc[i], drivetime$Hosp_loc[i], mode = "driving", override_limit = TRUE)
Sys.sleep(10)
}
res_df <- do.call(rbind, res)
结果:
> res_df
from to m km miles seconds minutes hours
1 54.571614 -1.232274 52.45327 -1.936283 286142 286.142 177.8086 11061 184.3500 3.072500
2 54.53808 -1.292145 52.45327 -1.936283 281690 281.690 175.0422 10908 181.8000 3.030000
3 54.570665 -1.167559 52.45327 -1.936283 291233 291.233 180.9722 11244 187.4000 3.123333
4 54.563124 -.980768 52.45327 -1.936283 302302 302.302 187.8505 11748 195.8000 3.263333
5 54.561689 -1.318938 52.45327 -1.936283 287334 287.334 178.5493 11079 184.6500 3.077500
6 54.682334 -1.214149 52.45327 -1.936283 301669 301.669 187.4571 11843 197.3833 3.289722
7 54.571908 -1.226236 52.45327 -1.936283 286680 286.680 178.1430 10956 182.6000 3.043333
8 54.561397 -1.321186 52.45327 -1.936283 287460 287.460 178.6276 11109 185.1500 3.085833
9 54.696642 -1.183105 52.45327 -1.936283 309272 309.272 192.1816 12062 201.0333 3.350556
10 54.57537 -1.236504 52.45327 -1.936283 285644 285.644 177.4992 11001 183.3500 3.055833