[R] Managing NA values in aggregate

Jeff Newmiller jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Sat Sep 4 18:21:38 CEST 2021


Literal translation...

aggregate( mydf$value
         , mydf[ , "year_month", drop=FALSE ]
         , function( x ) if ( all( is.na( x ) ) ) NA else sum( x, na.rm = TRUE )
         )

On September 4, 2021 9:06:57 AM PDT, Stefano Sofia <stefano.sofia using regione.marche.it> wrote:
>Dear R-list users,
>I encountered a silly problem using aggregate, but I am not able to get rid of it.
>I read the manual quite carefully, probaby not enough.
>Suppose I have the following data frame:
>
>mydf <- data.frame(year_month=rep(c("2003-12", "2004-12", "2005-12"), 3), station_number=c(rep(1818,3), rep(1819,3), rep(1820,3)), value=c(NA, NA, 20, NA, NA, 40, NA, 15, 50))
>
>I want aggregate the column value by the column year_month, but I need to preserve rows with NA when all data are NA:
>
>aggregate(mydf$value, list(mydf$year_month), sum) gives
>1 2003-12  NA
>2 2004-12  NA
>3 2005-12 110
>
>aggregate(mydf$value, list(mydf$year_month), sum, na.rm=TRUE) gives
>1 2003-12   0
>2 2004-12  15
>3 2005-12 110
>
>but I need
>1 2003-12   NA
>2 2004-12  15
>3 2005-12 110
>
>How can I get it?
>Thank you for your attention and your help
>Stefano
>
>
>         (oo)
>--oOO--( )--OOo--------------------------------------
>Stefano Sofia PhD
>Civil Protection - Marche Region - Italy
>Meteo Section
>Snow Section
>Via del Colle Ameno 5
>60126 Torrette di Ancona, Ancona (AN)
>Uff: +39 071 806 7743
>E-mail: stefano.sofia using 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 Libraesva ESG ed  risultato non infetto.
>This message was scanned by Libraesva ESG and is believed to be clean.
>
>
>	[[alternative HTML version deleted]]
>
>______________________________________________
>R-help using 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.

-- 
Sent from my phone. Please excuse my brevity.



More information about the R-help mailing list