我们可以遍历行序列,然后使用该索引来子集“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)