Hello,
The problem is that the first aggregate's second column is a list and
the second aggregate's second column is a matrix.
In the code below I have complicated it a bit so that the intermediate
results are created and examined.
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))
agg <- aggregate(daily_mean ~ as.integer(format(mydf$Data, "%Y")),
data=mydf, cumsum)
mydf$yearly_sum <- unlist(agg[2], use.names = FALSE)
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))
dim(mydf1)
#> [1] 7670 2
# this removes 5 rows from mydf1
i <- format(mydf1$Data, "%m-%d") != "02-29"
mydf1 <- mydf1[i, ]
dim(mydf1)
#> [1] 7665 2
agg2 <- aggregate(daily_mean ~ as.integer(format(mydf1$Data, "%Y")),
data=mydf1, cumsum)
mydf1$yearly_sum <- unlist(agg2[2], use.names = FALSE)
Now see what is in agg and in agg2.
class(agg$daily_mean)
#> [1] "list"
# returns FALSE, 5 list members have length 366
all(lengths(agg$daily_mean) == 365)
#> [1] FALSE
lengths(agg$daily_mean)
#> [1] 365 366 365 365 365 366 365 365 365 366 365 365 365 366 365
365 365 366 365
#> [20] 365 365
class(agg2$daily_mean)
#> [1] "matrix" "array"
ncol(agg2$daily_mean) == 365
#> [1] TRUE
agg2's second column is a matrix where each row represents the year's
cumulative sums. R stores matrices in column-first order so you have
to transpose the matrix and then remove the dim attribute (for
instance, with `c`), not `unlist` it.
# after running the agg2 <- aggregate(...) above, run
mydf1$yearly_sum <- c(t(agg2$daily_mean))
Hope this helps,
Rui Barradas
Stefano Sofia via R-help <[email protected]> escreveu (segunda,
10/08/2026 à(s) 11:07):
>
> 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.