Dear R-list users,
I know that this is a trivial question, but I already wasted quite a large amount of time on that.
I have a data frame with daily data of snow fall. I need to evaluate the monthly mean for each month and each year in the data frame.
Could you help me to find an eficient way for these calculations?
Thank you for your help
Stefano
Here a reproducibile code:
first_day_1 <- as.POSIXct("2019-01-19-00-00", format="%Y-%m-%d-%H-%M")
last_day_1 <- as.POSIXct("2019-03-20-00-00", format="%Y-%m-%d-%H-%M")
df1 <- data.frame(data_POSIX=seq(first_day_1, last_day_1, by="1 days"))
df1$value <- runif(nrow(df1), 0, 10)
first_day_2 <- as.POSIXct("2020-02-19-00-00", format="%Y-%m-%d-%H-%M")
last_day_2 <- as.POSIXct("2020-04-20-00-00", format="%Y-%m-%d-%H-%M")
df2 <- data.frame(data_POSIX=seq(first_day_2, last_day_2, by="1 days"))
df2$value <- runif(nrow(df2), 0, 10)
df <- rbind(df1, df2)
(oo)
--oOO--( )--OOo----------------
Stefano Sofia PhD
Civil Protection - Marche Region
Meteo Section
Snow Section
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.
monthly mean for each month and each year in the data frame
5 messages · Jim Lemon, Rui Barradas, Stefano Sofia
Hi Stefano, What about this? df$months<-format(df$data_POSIX,"%Y-%m") snow_means<-by(df$value,df$months,mean) Jim On Mon, Sep 14, 2020 at 8:19 PM Stefano Sofia
<stefano.sofia at regione.marche.it> wrote:
Dear R-list users,
I know that this is a trivial question, but I already wasted quite a large amount of time on that.
I have a data frame with daily data of snow fall. I need to evaluate the monthly mean for each month and each year in the data frame.
Could you help me to find an eficient way for these calculations?
Thank you for your help
Stefano
Here a reproducibile code:
first_day_1 <- as.POSIXct("2019-01-19-00-00", format="%Y-%m-%d-%H-%M")
last_day_1 <- as.POSIXct("2019-03-20-00-00", format="%Y-%m-%d-%H-%M")
df1 <- data.frame(data_POSIX=seq(first_day_1, last_day_1, by="1 days"))
df1$value <- runif(nrow(df1), 0, 10)
first_day_2 <- as.POSIXct("2020-02-19-00-00", format="%Y-%m-%d-%H-%M")
last_day_2 <- as.POSIXct("2020-04-20-00-00", format="%Y-%m-%d-%H-%M")
df2 <- data.frame(data_POSIX=seq(first_day_2, last_day_2, by="1 days"))
df2$value <- runif(nrow(df2), 0, 10)
df <- rbind(df1, df2)
(oo)
--oOO--( )--OOo----------------
Stefano Sofia PhD
Civil Protection - Marche Region
Meteo Section
Snow Section
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.
Yes, perfect.
Thank you.
Stefano
(oo)
--oOO--( )--OOo----------------
Stefano Sofia PhD
Civil Protection - Marche Region
Meteo Section
Snow Section
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? 14 settembre 2020 12.29
A: Stefano Sofia
Cc: r-help mailing list
Oggetto: Re: [R] monthly mean for each month and each year in the data frame
Hi Stefano,
What about this?
df$months<-format(df$data_POSIX,"%Y-%m")
snow_means<-by(df$value,df$months,mean)
Jim
On Mon, Sep 14, 2020 at 8:19 PM Stefano Sofia
<stefano.sofia at regione.marche.it> wrote:
>
> Dear R-list users,
> I know that this is a trivial question, but I already wasted quite a large amount of time on that.
> I have a data frame with daily data of snow fall. I need to evaluate the monthly mean for each month and each year in the data frame.
> Could you help me to find an eficient way for these calculations?
>
> Thank you for your help
> Stefano
>
> Here a reproducibile code:
>
> first_day_1 <- as.POSIXct("2019-01-19-00-00", format="%Y-%m-%d-%H-%M")
> last_day_1 <- as.POSIXct("2019-03-20-00-00", format="%Y-%m-%d-%H-%M")
> df1 <- data.frame(data_POSIX=seq(first_day_1, last_day_1, by="1 days"))
> df1$value <- runif(nrow(df1), 0, 10)
>
>
> first_day_2 <- as.POSIXct("2020-02-19-00-00", format="%Y-%m-%d-%H-%M")
> last_day_2 <- as.POSIXct("2020-04-20-00-00", format="%Y-%m-%d-%H-%M")
> df2 <- data.frame(data_POSIX=seq(first_day_2, last_day_2, by="1 days"))
> df2$value <- runif(nrow(df2), 0, 10)
>
> df <- rbind(df1, df2)
>
>
>
>
>
> (oo)
> --oOO--( )--OOo----------------
> Stefano Sofia PhD
> Civil Protection - Marche Region
> Meteo Section
> Snow Section
> 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.
Hello, Here are two aggregate options, the first base R only, the second one with package zoo. year_month <- format(df$data_POSIX, "%Y-%m") aggregate(value ~ year_month, df, mean) # or even value ~ format(etc) # year_month value #1 2019-01 6.430922 #2 2019-02 4.846731 #3 2019-03 4.968503 #4 2020-02 6.484031 #5 2020-03 4.910305 #6 2020-04 4.906575 aggregate(value ~ zoo::as.yearmon(data_POSIX), df, mean) # zoo::as.yearmon(data_POSIX) value #1 jan 2019 6.430922 #2 fev 2019 4.846731 #3 mar 2019 4.968503 #4 fev 2020 6.484031 #5 mar 2020 4.910305 #6 abr 2020 4.906575 Hope this helps, Rui Barradas ?s 11:19 de 14/09/20, Stefano Sofia escreveu:
Dear R-list users,
I know that this is a trivial question, but I already wasted quite a large amount of time on that.
I have a data frame with daily data of snow fall. I need to evaluate the monthly mean for each month and each year in the data frame.
Could you help me to find an eficient way for these calculations?
Thank you for your help
Stefano
Here a reproducibile code:
first_day_1 <- as.POSIXct("2019-01-19-00-00", format="%Y-%m-%d-%H-%M")
last_day_1 <- as.POSIXct("2019-03-20-00-00", format="%Y-%m-%d-%H-%M")
df1 <- data.frame(data_POSIX=seq(first_day_1, last_day_1, by="1 days"))
df1$value <- runif(nrow(df1), 0, 10)
first_day_2 <- as.POSIXct("2020-02-19-00-00", format="%Y-%m-%d-%H-%M")
last_day_2 <- as.POSIXct("2020-04-20-00-00", format="%Y-%m-%d-%H-%M")
df2 <- data.frame(data_POSIX=seq(first_day_2, last_day_2, by="1 days"))
df2$value <- runif(nrow(df2), 0, 10)
df <- rbind(df1, df2)
(oo)
--oOO--( )--OOo----------------
Stefano Sofia PhD
Civil Protection - Marche Region
Meteo Section
Snow Section
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.
Thank you again.
I finally used aggregate because it build a data frame straight away.
Stefano
(oo)
--oOO--( )--OOo----------------
Stefano Sofia PhD
Civil Protection - Marche Region
Meteo Section
Snow Section
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: Rui Barradas [ruipbarradas at sapo.pt]
Inviato: luned? 14 settembre 2020 12.53
A: Stefano Sofia; r-help mailing list
Oggetto: Re: [R] monthly mean for each month and each year in the data frame
Hello,
Here are two aggregate options, the first base R only, the second one
with package zoo.
year_month <- format(df$data_POSIX, "%Y-%m")
aggregate(value ~ year_month, df, mean) # or even value ~ format(etc)
# year_month value
#1 2019-01 6.430922
#2 2019-02 4.846731
#3 2019-03 4.968503
#4 2020-02 6.484031
#5 2020-03 4.910305
#6 2020-04 4.906575
aggregate(value ~ zoo::as.yearmon(data_POSIX), df, mean)
# zoo::as.yearmon(data_POSIX) value
#1 jan 2019 6.430922
#2 fev 2019 4.846731
#3 mar 2019 4.968503
#4 fev 2020 6.484031
#5 mar 2020 4.910305
#6 abr 2020 4.906575
Hope this helps,
Rui Barradas
?s 11:19 de 14/09/20, Stefano Sofia escreveu:
> Dear R-list users,
> I know that this is a trivial question, but I already wasted quite a large amount of time on that.
> I have a data frame with daily data of snow fall. I need to evaluate the monthly mean for each month and each year in the data frame.
> Could you help me to find an eficient way for these calculations?
>
> Thank you for your help
> Stefano
>
> Here a reproducibile code:
>
> first_day_1 <- as.POSIXct("2019-01-19-00-00", format="%Y-%m-%d-%H-%M")
> last_day_1 <- as.POSIXct("2019-03-20-00-00", format="%Y-%m-%d-%H-%M")
> df1 <- data.frame(data_POSIX=seq(first_day_1, last_day_1, by="1 days"))
> df1$value <- runif(nrow(df1), 0, 10)
>
>
> first_day_2 <- as.POSIXct("2020-02-19-00-00", format="%Y-%m-%d-%H-%M")
> last_day_2 <- as.POSIXct("2020-04-20-00-00", format="%Y-%m-%d-%H-%M")
> df2 <- data.frame(data_POSIX=seq(first_day_2, last_day_2, by="1 days"))
> df2$value <- runif(nrow(df2), 0, 10)
>
> df <- rbind(df1, df2)
>
>
>
>
>
> (oo)
> --oOO--( )--OOo----------------
> Stefano Sofia PhD
> Civil Protection - Marche Region
> Meteo Section
> Snow Section
> 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.