多步循环,用于获取多年和站点的天气数据(Multi step loop to acquire weather data over years and stations)

编程入门 行业动态 更新时间:2024-10-28 14:24:59
多步循环,用于获取多年和站点的天气数据(Multi step loop to acquire weather data over years and stations)

我有一个过程,可以在一个月内为单个气象站创建一个df。 但是,我有大约25个站点,我想获取超过5年的降水数据。

我有一个df的站点ID,看起来像下面的表格(但是还有23个站点。

stationid County GHCND:USW00093721 ANNEARUNDEL GHCND:USC00182308 BALTIMORE

通过以下代码获取天气数据集

library("rnoaa") ANNEARUNDEL_2006 <- ncdc(datasetid='GHCND', stationid = "GHCND:USC00182060", datatypeid='PRCP', startdate = '2006-07-01', enddate = '2006-08-01', limit=400, token = "API KEY") ANNEARUNDEL_2006 <- ANNEARUNDEL_2006$data

我熟悉适用于一个进程的非常基本的for循环。 有没有办法设置这个循环会使用县名和2006年到2011年的所有25个站创建一个新的df? 循环是实现此目的的最佳方法吗?

I have a process to create a df for a single weather station over a singular month period. However, I have about 25 stations that I would like to aquire precipitation data for over a 5 year period.

I have the station ids in a df, that looks like the table below (but with 23 more stations.

stationid County GHCND:USW00093721 ANNEARUNDEL GHCND:USC00182308 BALTIMORE

The weather dataset is aquired through the following code

library("rnoaa") ANNEARUNDEL_2006 <- ncdc(datasetid='GHCND', stationid = "GHCND:USC00182060", datatypeid='PRCP', startdate = '2006-07-01', enddate = '2006-08-01', limit=400, token = "API KEY") ANNEARUNDEL_2006 <- ANNEARUNDEL_2006$data

I familiar with very basic for loops that work for one process. Is there a way to set this up the loop would create a new df using the county name and year over the span of 2006 to 2011 for all 25 stations? Is a loop the best way to accomplish this?

最满意答案

我喜欢这样的循环,因为它们更容易阅读和书写。 你可以用两个循环这样做:

my_df <- read.table(text = "stationid County GHCND:USW00093721 ANNEARUNDEL GHCND:USC00182308 BALTIMORE", header = T) library(rnoaa) results <- list() # list as storage variable for the loop results i <- 1 # indexing variable for(sid in unique(my_df$stationid)) { # each station in your stationid dataframe for(year in 2006:2011) { # each year you care about data <- ncdc(datasetid='GHCND', stationid = sid, datatypeid='PRCP', startdate = paste0(year, '-01-01'), enddate = paste0(year, '-12-31'), limit=400, token = "API KEY")$data # subset the returned list right away here with $data # add info from each loop iteration data$county <- my_df[my_df$stationid == sid,]$County data$year <- year results[[i]] <- data # store it i <- i + 1 # rinse and repeat } } one_big_df <- do.call(rbind, results) # stack all of the data frames together rowwise

当然,您可以随时调整for循环使用lapply或它的朋友。 如果速度成为问题,您可能需要考虑它。

I like loops for things like this because they are easier to read and write. You could do it like this with two loops:

my_df <- read.table(text = "stationid County GHCND:USW00093721 ANNEARUNDEL GHCND:USC00182308 BALTIMORE", header = T) library(rnoaa) results <- list() # list as storage variable for the loop results i <- 1 # indexing variable for(sid in unique(my_df$stationid)) { # each station in your stationid dataframe for(year in 2006:2011) { # each year you care about data <- ncdc(datasetid='GHCND', stationid = sid, datatypeid='PRCP', startdate = paste0(year, '-01-01'), enddate = paste0(year, '-12-31'), limit=400, token = "API KEY")$data # subset the returned list right away here with $data # add info from each loop iteration data$county <- my_df[my_df$stationid == sid,]$County data$year <- year results[[i]] <- data # store it i <- i + 1 # rinse and repeat } } one_big_df <- do.call(rbind, results) # stack all of the data frames together rowwise

Of course, you could always adjust a for loop to using lapply or it's friends. If speed became an issue you might want to consider it.

更多推荐

本文发布于:2023-07-28 23:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1310293.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多年   天气   站点   数据   Multi

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!