Stefano Sofia
2018-Aug-10 16:48 UTC
[R] cumulate of snow cumulates from daily values of different automatic stations for some time intervals
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 at 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]]
Jim Lemon
2018-Aug-12 23:55 UTC
[R] cumulate of snow cumulates from daily values of different automatic stations for some time intervals
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 at 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 at 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 at 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.
Stefano Sofia
2018-Aug-16 10:33 UTC
[R] cumulate of snow cumulates from daily values of different automatic stations for some time intervals
Hi Jim. Thank you for your help. I found very useful cum_snow and cum_list, but I decided to manage the list and dates in a different way. First of all I decided to deal with a unique data frame (called df_CFS) where I attached all the 10 data frames, and instead to build a list with the 10 different data frames I created a list with the 10 station codes (217, 2018, ...): list_station_code <- list(217, 218, 219, ...) For managing dates, I created a vector of length 6. This is an example my_date <- c("1999-12-17-00-00", "2000-01-07-00-00", "2000-01-10-00-00", "2000-01-15-00-00", NA, NA) and then I created three functions based on the non NA elements of my_date: sum_prec1 <- function(x, init_day1_POSIX, fin_day1_POSIX) { sum(df_CFS$Hn[df_CFS$Codice_sensore==x & df_CFS$data_POSIX >= init_day1_POSIX & df_CFS$data_POSIX <= fin_day1_POSIX], na.rm=T) } sum_prec2 <- function(x, init_day1_POSIX, fin_day1_POSIX, init_day2_POSIX, fin_day2_POSIX) { print("sum_prec2") sum1 <- sum(df_CFS$Hn[df_CFS$Codice_sensore==x & df_CFS$data_POSIX >= init_day1_POSIX & df_CFS$data_POSIX <= fin_day1_POSIX], na.rm=T) sum2 <- sum(df_CFS$Hn[df_CFS$Codice_sensore==x & df_CFS$data_POSIX >= init_day2_POSIX & df_CFS$data_POSIX <= fin_day2_POSIX], na.rm=T) sum <- sum1 + sum2 } sum_prec3 <- function(x, init_day1_POSIX, fin_day1_POSIX, init_day2_POSIX, fin_day2_POSIX, init_day3_POSIX, fin_day3_POSIX) { print("sum_prec3") sum1 <- sum(df_CFS$Hn[df_CFS$Codice_sensore==x & df_CFS$data_POSIX >= init_day1_POSIX & df_CFS$data_POSIX <= fin_day1_POSIX], na.rm=T) sum2 <- sum(df_CFS$Hn[df_CFS$Codice_sensore==x & df_CFS$data_POSIX >= init_day2_POSIX & df_CFS$data_POSIX <= fin_day2_POSIX], na.rm=T) sum3 <- sum(df_CFS$Hn[df_CFS$Codice_sensore==x & df_CFS$data_POSIX >= init_day3_POSIX & df_CFS$data_POSIX <= fin_day3_POSIX], na.rm=T) sum <- sum1 + sum2 + sum3 } Finally my_date_POSIX <- as.POSIXct(my_date), format="%Y-%m-%d-%H-%M") my_date_POSIX <- my_dates[!is.na(my_date_POSIX)] if (length(my_date_POSIX)==2) my_output <- lapply(list_station_code, sum_prec1, my_date_POSIX[[1]], my_date_POSIX[[2]]) else if (length(my_date_POSIX)==4) my_output <- lapply(list_station_code, sum_prec2, my_date_POSIX[[1]], my_date_POSIX[[2]], my_date_POSIX[[3]], my_date_POSIX[[4]]) else if (length(my_dates)==6) my_output <- lapply(list_station_code, sum_prec3, my_date_POSIX[[1]], my_date_POSIX[[2]], my_date_POSIX[[3]], my_date_POSIX[[4]], my_date_POSIX[[5]], my_date_POSIX[[6]]) df_snow_totals <- data.frame("station_code" = c(217, 218, 219)) df_snow_totals$Cumulata <- as.vector(my_output) df_snow_totals$Cumulata <- as.numeric(as.character(unlist(df_snow_totals$Cumulata))) It works. Thank you for 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 at regione.marche.it ---Oo---------oO---------------- ________________________________________ Da: Jim Lemon [drjimlemon at gmail.com] Inviato: luned? 13 agosto 2018 1.55 A: Stefano Sofia Cc: r-help at r-project.org Oggetto: Re: [R] cumulate of snow cumulates from daily values of different automatic stations for some time intervals 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 at 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 at 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 at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://urlsand.esvalabs.com/?u=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&e=52342f8a&h=d46bc785&f=y&p=y > PLEASE do read the posting guide https://urlsand.esvalabs.com/?u=http%3A%2F%2Fwww.R-project.org%2Fposting-guide.html&e=52342f8a&h=9b25bfd5&f=y&p=y > and provide commented, minimal, self-contained, reproducible code.-- Questo messaggio stato analizzato con Libra ESVA ed risultato non infetto. ________________________________ 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.