[R] cumulate of snow cumulates from daily values of different automatic stations for some time intervals
Jim Lemon
drj|m|emon @end|ng |rom gm@||@com
Mon Aug 13 01:55:28 CEST 2018
Hi Stefano,
This was such a stinker of a problem that I just had to crack it:
# create some data the lazy man's way
year_dates<-c(paste(2000,rep("01",31),formatC(1:31,width=2,flag=0),sep="-"),
paste(2000,rep("02",29),formatC(1:29,width=2,flag=0),sep="-"),
paste(2000,rep("03",31),formatC(1:31,width=2,flag=0),sep="-"),
paste(2000,rep("04",30),formatC(1:30,width=2,flag=0),sep="-"),
paste(2000,rep("05",31),formatC(1:31,width=2,flag=0),sep="-"),
paste(2000,rep("06",30),formatC(1:30,width=2,flag=0),sep="-"),
paste(2000,rep("07",31),formatC(1:31,width=2,flag=0),sep="-"),
paste(2000,rep("08",31),formatC(1:31,width=2,flag=0),sep="-"),
paste(2000,rep("09",30),formatC(1:30,width=2,flag=0),sep="-"),
paste(2000,rep("10",31),formatC(1:31,width=2,flag=0),sep="-"),
paste(2000,rep("11",30),formatC(1:30,width=2,flag=0),sep="-"),
paste(2000,rep("12",31),formatC(1:31,width=2,flag=0),sep="-"))
df1<-data.frame(station_code=rep(217,366),
date_factor=year_dates,date_POSIX=year_dates,
snow=c(sample(0:70,31),sample(0:50,29),sample(0:10,31,TRUE),rep(0,214),
sample(0:20,30,TRUE),sample(0:60,31)))
df2<-data.frame(station_code=rep(218,366),
date_factor=year_dates,date_POSIX=year_dates,
snow=c(sample(0:70,31),sample(0:50,29),sample(0:10,31,TRUE),rep(0,214),
sample(0:20,30,TRUE),sample(0:60,31)))
df3<-data.frame(station_code=rep(219,366),
date_factor=year_dates,date_POSIX=year_dates,
snow=c(sample(0:70,31),sample(0:50,29),sample(0:10,31,TRUE),rep(0,214),
sample(0:20,30,TRUE),sample(0:60,31)))
df4<-data.frame(station_code=rep(220,366),
date_factor=year_dates,date_POSIX=year_dates,
snow=c(sample(0:70,31),sample(0:50,29),sample(0:10,31,TRUE),rep(0,214),
sample(0:20,30,TRUE),sample(0:60,31)))
df5<-data.frame(station_code=rep(221,366),
date_factor=year_dates,date_POSIX=year_dates,
snow=c(sample(0:70,31),sample(0:50,29),sample(0:10,31,TRUE),rep(0,214),
sample(0:20,30,TRUE),sample(0:60,31)))
df6<-data.frame(station_code=rep(222,366),
date_factor=year_dates,date_POSIX=year_dates,
snow=c(sample(0:70,31),sample(0:50,29),sample(0:10,31,TRUE),rep(0,214),
sample(0:20,30,TRUE),sample(0:60,31)))
df7<-data.frame(station_code=rep(223,366),
date_factor=year_dates,date_POSIX=year_dates,
snow=c(sample(0:70,31),sample(0:50,29),sample(0:10,31,TRUE),rep(0,214),
sample(0:20,30,TRUE),sample(0:60,31)))
df8<-data.frame(station_code=rep(224,366),
date_factor=year_dates,date_POSIX=year_dates,
snow=c(sample(0:70,31),sample(0:50,29),sample(0:10,31,TRUE),rep(0,214),
sample(0:20,30,TRUE),sample(0:60,31)))
df9<-data.frame(station_code=rep(225,366),
date_factor=year_dates,date_POSIX=year_dates,
snow=c(sample(0:70,31),sample(0:50,29),sample(0:10,31,TRUE),rep(0,214),
sample(0:20,30,TRUE),sample(0:60,31)))
df10<-data.frame(station_code=rep(226,366),
date_factor=year_dates,date_POSIX=year_dates,
snow=c(sample(0:70,31),sample(0:50,29),sample(0:10,31,TRUE),rep(0,214),
sample(0:20,30,TRUE),sample(0:60,31)))
snow_list<-list(df1,df2,df3,df4,df5,df6,df7,df8,df9,df10)
for(station in 1:10)
snow_list[[station]]$doy<-1:length(snow_list[[station]]$date_POSIX)
select_days<-c(1:12,83:88)
cum_snow<-function(x,which_days) {
return(list(x$station_code[1],sum(x$snow[which_days])))
}
cum_list<-lapply(lapply(snow_list,cum_snow,select_days),unlist)
snow_totals<-data.frame(station_code=NULL,snow_cumulate=NULL)
for(station in 1:10) snow_totals<-rbind(snow_totals,cum_list[[station]])
names(snow_totals)<-c("station_code","snow_cumulate")
Jim
On Sat, Aug 11, 2018 at 2:48 AM, Stefano Sofia
<stefano.sofia using regione.marche.it> wrote:
> Dear R-list users,
> I have 10 data frames (called df1, df2, ... df10), where each of them contains snow data from an automatic meteorological station (obviously each station has a different station code).
> Here is an example of df1:
>
> station_code date_factor date_POSIX snow
> 217 1999-12-15 1999-12-15 0
> 217 1999-12-16 1999-12-16 0
> 217 1999-12-17 1999-12-17 38
> 217 1999-12-18 1999-12-18 31
> 217 1999-12-19 1999-12-19 21
> 217 1999-12-20 1999-12-20 12
> 217 1999-12-21 1999-12-21 42
> 217 1999-12-22 1999-12-22 61
> 217 1999-12-23 1999-12-23 57
> 217 1999-12-24 1999-12-24 48
> ...
>
> where
>> sapply(df1, class)
> $station_code
> [1] "numeric"
>
> $date_factor
> [1] "factor"
>
> $date_POSIX
> [1] "POSIXct" "POSIXt"
>
> $snow
> [1] "integer"
>
> Given a series of max three intervals (example with two intervals: from 1st to 12th of January 2000 and from 23rd to 28th of March 2000), I need to evaluate for each station the total snow cumulate for all the intervals selected, and finally create a data frame where for each line there is the station code and the snow cumulate. It should be like
>
> station_code total_snow_cumulate
> 217 125
> 218 80
> ...
>
> Could somebody show me a direction for an efficient solution?
>
> Thank you for your attention and your help
> Stefano
>
>
> (oo)
> --oOO--( )--OOo----------------
> Stefano Sofia PhD
> Area Meteorologica e Area nivologica - Centro Funzionale
> Servizio Protezione Civile - Regione Marche
> Via del Colle Ameno 5
> 60126 Torrette di Ancona, Ancona
> Uff: 071 806 7743
> E-mail: stefano.sofia using regione.marche.it
> ---Oo---------oO----------------
>
> ________________________________
>
> AVVISO IMPORTANTE: Questo messaggio di posta elettronica può contenere informazioni confidenziali, pertanto è destinato solo a persone autorizzate alla ricezione. I messaggi di posta elettronica per i client di Regione Marche possono contenere informazioni confidenziali e con privilegi legali. Se non si è il destinatario specificato, non leggere, copiare, inoltrare o archiviare questo messaggio. Se si è ricevuto questo messaggio per errore, inoltrarlo al mittente ed eliminarlo completamente dal sistema del proprio computer. Ai sensi dell’art. 6 della DGR n. 1394/2008 si segnala che, in caso di necessità ed urgenza, la risposta al presente messaggio di posta elettronica può essere visionata da persone estranee al destinatario.
> IMPORTANT NOTICE: This e-mail message is intended to be received only by persons entitled to receive the confidential information it may contain. E-mail messages to clients of Regione Marche may contain information that is confidential and legally privileged. Please do not read, copy, forward, or store this message unless you are an intended recipient of it. If you have received this message in error, please forward it to the sender and delete it completely from your computer system.
>
> --
> Questo messaggio stato analizzato da Libra ESVA ed risultato non infetto.
> This message was scanned by Libra ESVA and is believed to be clean.
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list