0

I am using the new R package RNOAA to access climate data from NOAA and currently am doing it one time period at a time. Like this.

PRCP_2002a<- ncdc(datasetid='GHCND', datatypeid ='PRCP', startdate = '2001-08-13', enddate = '2002-02-13', stationid='x', token = 'x', limit = 500)

I end up with a long list of the above code with many different start and stop dates entered manually with one line of code for each time period. Is there a way to use a for loop and lapply or something similar with each start and stop date being grabbed from a data frame or list and the result object being named also from the list or data frame? I would presumably read a csv file like this in.

Periods <- read.csv("Climate_dates.csv")
         ID      Start       Stop
1 2002S 2001-08-13 2002-02-13
2 2002F 2002-04-02 2002-10-02
3 2003S 2002-09-19 2003-03-19
4 2003F 2003-04-22 2003-10-22
5 2004S 2003-09-30 2004-03-31
6 2004F 2004-04-20 2004-10-20
7 2005S 2004-09-23 2005-03-23
8 2005F 2005-04-26 2005-10-26

Then some method to refer to the dates and IDs sequentially as the for loop or lapply or whatever iterates through.

ID in Periods<- ncdc(datasetid='GHCND', datatypeid ='PRCP', startdate = 'Start in Periods', enddate = 'Stop in Periods', stationid='x', token = 'x', limit = 500)

Any help is greatly appreciated. Apologies for my R ineptitude.

Pierre suggested this solution however it appears not to be providing the start or end dates to the ncdc command. Any thoughts?

mylst <- apply(Periods[-1], 1, function(x) ncdc(datasetid='GHCND', datatypeid     ='PRCP', startdate = x[1], enddate = x[2], stationid='x', token =     'x', limit = 500))

Warning messages:
1: Error: (400) - Required parameter 'startdate' is missing. 
2: Error: (400) - Required parameter 'startdate' is missing. 
3: Error: (400) - Required parameter 'startdate' is missing. 
4: Error: (400) - Required parameter 'startdate' is missing. 
5: Error: (400) - Required parameter 'startdate' is missing. 
6: Error: (400) - Required parameter 'startdate' is missing. 
7: Error: (400) - Required parameter 'startdate' is missing. 
8: Error: (400) - Required parameter 'startdate' is missing.
www
  • 38,575
  • 12
  • 48
  • 84
  • For those who do not have the package installed, please post some of the data in question and the desired result. – Pierre L Nov 11 '16 at 16:38
  • Try `mylst <- apply(Periods[-1], 1, function(x) ncdc(datasetid='GHCND', datatypeid ='PRCP', startdate = x[1], enddate = x[2], stationid='x', token = 'x', limit = 500))`. Then you can name it afterwards, `names(mylist) <- Periods[,1]` – Pierre L Nov 11 '16 at 17:19
  • it says on the NOAA website there is a 5 second time limit between requests, you might do better by just reading them in a loop, or defining a new function with a time delay and using apply – Allen Wang Nov 11 '16 at 17:26
  • I really appreciate your help Pierre. I think it's almost there. As per edit above it seems the code you suggested is not getting the start or stop dates. Any thoughts? Should the code read startdate = x[2], enddate = x[3] – Nathan Jones Nov 14 '16 at 16:49

0 Answers0