Dear R-list users,
I've got problems to use the function aggregate.
Here there is an example:
mydf <- data.frame(Data=seq(as.POSIXct("2003-01-01", format = "%Y-%m-%d",
tz="Etc/GMT-1"), as.POSIXct("2023-12-31", format = "%Y-%m-%d", tz="Etc/GMT-1"),
by="1 day"), daily_mean = round(runif(7670, 0, 2), digits=2))
mydf$yearly_sum <- unlist(aggregate(daily_mean~as.integer(format(mydf$Data,
"%Y")), data=mydf, cumsum)[2], use.names = FALSE)
The column "yearly_sum" is the sum of the column "daily_mean" with a reset at
the beginning of each year.
If for my analysis I want to remove the 29th of February, "yearly_sum" does not
work anymore:
mydf1 <- data.frame(Data=seq(as.POSIXct("2003-01-01", format = "%Y-%m-%d",
tz="Etc/GMT-1"), as.POSIXct("2023-12-31", format = "%Y-%m-%d", tz="Etc/GMT-1"),
by="1 day"), daily_mean = round(runif(7670, 0, 2), digits=2))
mydf1 <- mydf1[format(mydf1$Data, "%m-%d") != "02-29", ]
mydf1$yearly_sum <- unlist(aggregate(daily_mean~as.integer(format(mydf1$Data,
"%Y")), data=mydf1, cumsum)[2], use.names = FALSE)
In this case the column "yearly_sum" does not sum the values, and honestly I do
not understand what is happening. Why?
Could somebody help me? I already spent a big amount of hours with no success.
Thank you for your attention and you help
Stefano
(oo)
--oOO--( )--OOo--------------------------------------
Stefano Sofia MSc, PhD
Civil Protection Department - Marche Region - Italy
Meteo Section
Snow Section
Via Colle Ameno 5
60126 Torrette di Ancona, Ancona (AN)
Uff: +39 071 806 7743
E-mail: [email protected]
---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. Ai sensi dell'art. 2.4 dell'allegato 1 alla DGR n. 74/2021, 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.
[[alternative HTML version deleted]]
______________________________________________
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.