我查看了幾個不同的相關問題:
- 錯誤時使用 tryCatch 跳到回圈的下一個值?
- 如何使用 Trycatch 跳過 R 中資料下載中的錯誤(這個可能與我的問題最相似)
我想遍歷資料幀中的坐標,并使用 USGS nhdplustools 包中的 discover_nhdplus_id() 函式來搜索最近的下坡流,并在矩陣中記錄該流的 ID 號 (COMID)。如果遇到錯誤,我希望回圈記錄“NA”并移動到下一個坐標。
示例資料框(這里有兩對坐標無法回傳結果:0,0 和 -111.2395, 36.5396):
# Minimal example dataframe
y <- c(38.27691,
38.440779,
37.784306,
0,
36.5396,
38.293296,
36.5375
)
x <- c(-112.64105,
-111.643221,
-111.633194,
0,
-111.2395,
-111.550817,
-111.2371
)
test_df <- data.frame(x, y)
理想情況下,輸出如下所示:
[1] 1215201
[1] 4900445
[1] 3277825
[1] NA
[1] NA
[1] 944070011
[1] 3528735
但是,當我實作 tryCatch() 時,遇到錯誤時回圈仍然崩潰。這些往往是“請求失敗 [500]”錯誤。這是我的 tryCatch() 嘗試:
library(nhdplusTools)
output <- matrix(NA, nrow=nrow(test_df), ncol=1)
colnames(output) <- c("COMID_raindrop")
for (i in 1:nrow(test_df)){
latitude <- test_df$y[i]
longitude <- test_df$x[i]
start_point <- st_sfc(st_point(c(longitude, latitude)), crs = 4269)
raindrop_trace <- tryCatch(
{
discover_nhdplus_id(start_point)
},
error = function(e) {
NA
}
)
output[i] <- raindrop_trace
print(raindrop_trace)
}
[1] 1215201
[1] 4900445
[1] 3277825
No data returned for: https://labs.waterdata.usgs.gov/api/nldi/linked-data/comid/position?coords=POINT(0%200%29FALSE
Request failed [500]. Retrying in 1.2 seconds...
Request failed [500]. Retrying in 1 seconds...
Error in: https://labs.waterdata.usgs.gov/api/nldi/linked-data/comid/NULL/
Error in output[i] <- raindrop_trace : replacement has length zero
在查找有關此的資訊時,我看到 purrr::possively 推薦,但是當我嘗試實作時回傳一個不同的錯誤:
output <- matrix(NA, nrow=nrow(test_df), ncol=1)
colnames(output) <- c("COMID_raindrop")
for (i in 1:nrow(test_df)){
latitude <- test_df$y[i]
longitude <- test_df$x[i]
start_point <- st_sfc(st_point(c(longitude, latitude)), crs = 4269)
get_data <- discover_nhdplus_id(start_point)
raindrop_trace <- purrr::possibly(get_data, otherwise = NA)
output[i] <- raindrop_trace
print(raindrop_trace)
}
Error in output[i] <- raindrop_trace :
incompatible types (from closure to logical) in subassignment type fix
我認為此錯誤可能與分配“NA”(非數字值)有關,但在分配 else = 0 時出現相同的錯誤。
任何幫助是極大的贊賞。
uj5u.com熱心網友回復:
您需要添加NA一個回傳函式以將其寫入您的輸出。
編輯:
您可以messages使用withCallingHandlers. 我添加了您有問題的坐標(評論)來測驗它。
library(nhdplusTools)
library(sf)
y <- c(38.27691,
38.440779,
37.784306,
0,
0,
38.293296,
36.5375,
36.5396)
x <- c(-112.64105,
-111.643221,
-111.633194,
0,
0,
-111.550817,
-111.2371,
-111.2395)
test_df <- data.frame(x, y)
test_df <- st_as_sf(x = test_df, coords = c("x", "y"), crs = 4269)
output <- matrix(NA, nrow=nrow(test_df), ncol=1)
colnames(output) <- c("COMID_raindrop")
for (i in seq_along(test_df[[1]])){
start_point <- test_df[i,]
raindrop_trace <- tryCatch(
{
withCallingHandlers(discover_nhdplus_id(point = start_point), message = function(c) if (inherits(c, "message")){stop("")})
discover_nhdplus_id(point = start_point)
},
error = function(e) {
return(NA)
}
)
output[i] <- raindrop_trace
print(raindrop_trace)
}
[1] 1215201
[1] 4900445
[1] 3277825
[1] NA
[1] NA
[1] 944070011
[1] 3528735
[1] NA
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/382746.html
