0

I am a new user to R and am trying to use the RNOAA package to get some average weather data for discrete periods of time.

I can get the data from the ncdc code in the RNOAA package and turn this into a data frame but I am having difficulty getting lapply to work for just the mean of the values for the time period.

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

this is done for a large number of time periods. I want to use lapply to extract a data frame from the 'ncdc dataset' object type that I receive from ncdc for each time period.

P1 <- PRCP_2002a$data

Prcp_list = list(P1, P2, P3, P4, etc)

Then use lapply to go over the list of data frames and calculate the mean of the fourth column (value) and append it to a list.

I tried this code from another help thread here but it gives me a list of lists which includes na's from all the metadata fields that the ncdc includes in the original object.

means<- lapply(Prcp_list, function (x) lapply(x,  mean,  na.rm= TRUE)) 

Any help is greatly appreciated and I apologize in advance for any poor choices in code.

www
  • 38,575
  • 12
  • 48
  • 84
  • 1
    can you post the output from `dput(head(PRCP_2002a))`? you need a valid token from NCDC to utilize the code above. – Nate Nov 09 '16 at 20:19
  • You can try `means<- lapply(Prcp_list, function (x) mean(x[,4], na.rm= TRUE))` – HubertL Nov 09 '16 at 21:09
  • I removed the token code since they are user specific. – Nathan Jones Nov 10 '16 at 04:28
  • Output from dput(head(PRCP_2002a)) is too long to post.... – Nathan Jones Nov 10 '16 at 04:31
  • Thanks HubertL that works great! One follow up question if I may. Any advice on using lapply to do the data extraction or would a for loop work better? Currently using just P1 <- PRCP_2002a$data P2 <- PRCP_2002b$data P3 <- PRCP_2003a$data P4 <- PRCP_2003b$data etc.... – Nathan Jones Nov 10 '16 at 21:44

0 Answers0