1

我正在尝试检索特定的气候station_ID数据date。我的数据集包含这两个元素。

for(i in data$date){lapply(data$station_id[i],
                           ghcnd_search,
                           var = "PRCP", 
                           date_min = data$date[i],
                           date_max = data$date[i])}

我试过这个但无济于事。如果有人可以帮助我,我将不胜感激。

4

1 回答 1

0

我们可以遍历行序列,然后使用该索引来子集“date”和“station_id”的值

library(rnoaa)
out <- vector('list', nrow(data))
for(i in seq_len(nrow(data))){
 out[[i]] <- ghcnd_search(stationid  = data$station_id[i], 
   var = "PRCP", date_min = data$date[i], date_max = data$date[i])
}

-输出

out
#[[1]]
#[[1]]$prcp
# A tibble: 1 x 6
#  id           prcp date       mflag qflag sflag
#  <chr>       <int> <date>     <chr> <chr> <chr>
#1 AGE00147704    90 1920-01-01 " "   " "   E    


#[[2]]
#[[2]]$prcp
# A tibble: 1 x 6
#  id           prcp date       mflag qflag sflag
#  <chr>       <int> <date>     <chr> <chr> <chr>
#1 AGE00147704     0 1915-01-01 " "   " "   E    

或使用Map

out1 <- Map(function(x, y) ghcnd_search(stationid = x, var = "PRCP",
         date_min = y, date_max= y), data$station_id, data$date)

数据

data <- data.frame(station_id = c("AGE00147704", "AGE00147704"),
    date = c("1920-01-01", "1915-01-01"), stringsAsFactors = FALSE)
于 2020-06-24T23:00:32.303 回答