Re: Problems with the function aggregate

Gabor Grothendieck <[email protected]>
Newsgroups gmane.comp.lang.r.general
Message-ID <CAP01uRksuHAOj8Td+-L75O3N4fZjUWhM9RFKt6qp_i4soa0btA@mail.gmail.com>
The ave solution could be written as:

transform(mydf1, yearly_sum = ave(daily_mean, cut(Data, "year"), FUN = cumsum))

On Mon, Aug 10, 2026 at 6:43 AM Eric Berger <[email protected]> wrote:
>
> I asked ChatGPT (5.5 Medium). Here is its analysis
>
> The problem is not Feb. 29 itself. The problem is that aggregate()
> changes its return structure depending on whether all groups have the
> same length.
> In your first version, years have different numbers of days: most have
> 365, leap years have 366. So aggregate(...)
> returns a grouped result where each year’s cumsum() is kept as a list element.
> Then this works by accident:
> unlist(...[2], use.names = FALSE)
> because it flattens the list year by year.
> But after you remove Feb. 29, every year has exactly 365 rows. Now
> aggregate() sees that every cumsum() result has the same length, so it
> “simplifies” the result into a matrix-like structure. When you then
> unlist() it, the values are no longer flattened in the chronological
> order you expect.
> The clean fix is: do not use aggregate() for this. Use ave().
>
> 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 <- ave(
>   mydf1$daily_mean,
>   as.integer(format(mydf1$Data, "%Y")),
>   FUN = cumsum
> )
>
> You can also force your original aggregate() approach to behave more
> consistently by adding simplify = FALSE:
>
> tmp <- aggregate(
>   daily_mean ~ as.integer(format(mydf1$Data, "%Y")),
>   data = mydf1,
>   FUN = cumsum,
>   simplify = FALSE
> )
>
> mydf1$yearly_sum <- unlist(tmp$daily_mean, use.names = FALSE)
>
>
>
>
>
>
>
> On Mon, Aug 10, 2026 at 1:07 PM Stefano Sofia via R-help
> <[email protected]> wrote:
> >
> > 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.
>
> ______________________________________________
> [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.



-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com
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.