rnoaa包仅允许您一次收集30天的气压信息https://cran.r-project.org/web/packages/rnoaa/rnoaa.pdf。我希望创建一个function/ for循环来一次一个月地从包中提取数据。它特定于所需的日期格式YYYYMMDD。No -或者/。我从一个函数开始,但lapply似乎没有应用于调用气压数据的函数。
我用很多方法尝试过循环,但我似乎不能理解。下面是一个例子。
for (i in dates)) {
air_pressure[i] <- coops_search(begin_date = start[i], end_date = end[i],
station_name = 8727520, product= "air_pressure", units = "metric", time_zone = "gmt")
print(air_pressure[i])
}
start<-seq(as.Date("2015/01/01"), by = "month", length.out = 100)
start <- as.numeric(gsub("-","",start))
end<-seq(as.Date("2015/02/01"), by = "month", length.out = 100)
end <- as.numeric(gsub("-","",end))
pressure_function<- function(air_pressure) {
coops_search(station_name = 8727520, begin_date = starting,
end_date = ending, product = "air_pressure")
}
lapply(pressure_function, starting= start, ending= end, FUN= sum)没有真正的错误消息,只是不填充或运行函数。
发布于 2019-09-12 02:51:20
这里有一些非常基本的错误。首先,for循环有太多的闭括号。其次,您的lapply调用传递了一个函数作为第一个参数;如果不起作用,请在第二个插槽中传递它。还有更多……
不管怎样,试试这个:
library(rnoaa)
fun <- function(begin, end) {
coops_search(station_name = 8727520, begin_date = gsub("-", "", begin),
end_date = gsub("-", "", end), product = "air_pressure")
}
start_dates <- seq(as.Date("2015-01-01"), as.Date("2015-12-31"), by = "month")
end_dates <- seq(as.Date("2015-02-01"), as.Date("2016-01-01"), by = "month") - 1
res <- Map(fun, start_dates, end_dates)
df <- dplyr::bind_rows(lapply(res, "[[", "data"))
head(df)
#> t v f
#> 1 2015-01-01 00:00:00 1025.3 0,0,0
#> 2 2015-01-01 00:06:00 1025.4 0,0,0
#> 3 2015-01-01 00:12:00 1025.5 0,0,0
#> 4 2015-01-01 00:18:00 1025.6 0,0,0
#> 5 2015-01-01 00:24:00 1025.6 0,0,0
#> 6 2015-01-01 00:30:00 1025.6 0,0,0
NROW(df)
#> [1] 87600https://stackoverflow.com/questions/56138364
复制相似问题