Problems with the function aggregate

Stefano Sofia via R-help <[email protected]>
Newsgroups gmane.comp.lang.r.general
Message-ID <[email protected]>
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]]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.