Sum of columns of a data frame equal to NA when all the elements are NA

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Sum of columns of a data frame equal to NA when all the elements are NA

Stefano Sofia
Dear list users,
let me ask you this trivial question. I worked on that for a long time, by now.
Suppose to have a data frame with NAs and to sum some columns with rowSums:

df <- data.frame(A = runif(10), B = runif(10), C = rnorm(10))
df[1, ] <- NA
rowSums(df[ , which(names(df) %in% c("A","B"))], na.rm=T)

If all the elements of the selected columns are NA, rowSums returns 0 while I need NA.
Is there an easy and efficient way to use rowSums within a function like

function(x) ifelse(all(is.na(x)), as.numeric(NA), rowSums...)?

or an equivalent function?

Thank you for your help
Stefano



         (oo)
--oOO--( )--OOo----------------
Stefano Sofia PhD
Area Meteorologica e  Area nivologica - Centro Funzionale
Servizio Protezione Civile - Regione Marche
Via del Colle Ameno 5
60126 Torrette di Ancona, Ancona
Uff: 071 806 7743
E-mail: [hidden email]
---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 Libra ESVA ed  risultato non infetto.
This message was scanned by Libra ESVA and is believed to be clean.


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Sum of columns of a data frame equal to NA when all the elements are NA

Duncan Murdoch-2
On 21/03/2018 11:44 AM, Stefano Sofia wrote:

> Dear list users,
> let me ask you this trivial question. I worked on that for a long time, by now.
> Suppose to have a data frame with NAs and to sum some columns with rowSums:
>
> df <- data.frame(A = runif(10), B = runif(10), C = rnorm(10))
> df[1, ] <- NA
> rowSums(df[ , which(names(df) %in% c("A","B"))], na.rm=T)
>
> If all the elements of the selected columns are NA, rowSums returns 0 while I need NA.
> Is there an easy and efficient way to use rowSums within a function like
>
> function(x) ifelse(all(is.na(x)), as.numeric(NA), rowSums...)?
>
> or an equivalent function?

Something like

apply(df[,c("A", "B")], 1, function(x) if (all(is.na(x))) NA else sum(x,
na.rm = TRUE))

should do what you want.

Duncan Murdoch

>
> Thank you for your help
> Stefano
>
>
>
>           (oo)
> --oOO--( )--OOo----------------
> Stefano Sofia PhD
> Area Meteorologica e  Area nivologica - Centro Funzionale
> Servizio Protezione Civile - Regione Marche
> Via del Colle Ameno 5
> 60126 Torrette di Ancona, Ancona
> Uff: 071 806 7743
> E-mail: [hidden email]
> ---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 Libra ESVA ed  risultato non infetto.
> This message was scanned by Libra ESVA and is believed to be clean.
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Sum of columns of a data frame equal to NA when all the elements are NA

Boris Steipe
In reply to this post by Stefano Sofia
Should not the result be NULL if you have removed the NA with na.rm=TRUE ?

B.



> On Mar 21, 2018, at 11:44 AM, Stefano Sofia <[hidden email]> wrote:
>
> Dear list users,
> let me ask you this trivial question. I worked on that for a long time, by now.
> Suppose to have a data frame with NAs and to sum some columns with rowSums:
>
> df <- data.frame(A = runif(10), B = runif(10), C = rnorm(10))
> df[1, ] <- NA
> rowSums(df[ , which(names(df) %in% c("A","B"))], na.rm=T)
>
> If all the elements of the selected columns are NA, rowSums returns 0 while I need NA.
> Is there an easy and efficient way to use rowSums within a function like
>
> function(x) ifelse(all(is.na(x)), as.numeric(NA), rowSums...)?
>
> or an equivalent function?
>
> Thank you for your help
> Stefano
>
>
>
>         (oo)
> --oOO--( )--OOo----------------
> Stefano Sofia PhD
> Area Meteorologica e  Area nivologica - Centro Funzionale
> Servizio Protezione Civile - Regione Marche
> Via del Colle Ameno 5
> 60126 Torrette di Ancona, Ancona
> Uff: 071 806 7743
> E-mail: [hidden email]
> ---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 Libra ESVA ed  risultato non infetto.
> This message was scanned by Libra ESVA and is believed to be clean.
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Sum of columns of a data frame equal to NA when all the elements are NA

Jeff Newmiller
What do you mean by "should not"?

NULL means "missing object" in R. The result of the sum function is always expected to be numeric... so NA_real or NA_integer could make sense as possible return values. But you cannot compute on NULL so no, that doesn't work.

See the note under the "Value" section of ?sum as to why zero is returned when all inputs are removed.
--
Sent from my phone. Please excuse my brevity.

On March 21, 2018 9:03:29 AM PDT, Boris Steipe <[hidden email]> wrote:

>Should not the result be NULL if you have removed the NA with
>na.rm=TRUE ?
>
>B.
>
>
>
>> On Mar 21, 2018, at 11:44 AM, Stefano Sofia
><[hidden email]> wrote:
>>
>> Dear list users,
>> let me ask you this trivial question. I worked on that for a long
>time, by now.
>> Suppose to have a data frame with NAs and to sum some columns with
>rowSums:
>>
>> df <- data.frame(A = runif(10), B = runif(10), C = rnorm(10))
>> df[1, ] <- NA
>> rowSums(df[ , which(names(df) %in% c("A","B"))], na.rm=T)
>>
>> If all the elements of the selected columns are NA, rowSums returns 0
>while I need NA.
>> Is there an easy and efficient way to use rowSums within a function
>like
>>
>> function(x) ifelse(all(is.na(x)), as.numeric(NA), rowSums...)?
>>
>> or an equivalent function?
>>
>> Thank you for your help
>> Stefano
>>
>>
>>
>>         (oo)
>> --oOO--( )--OOo----------------
>> Stefano Sofia PhD
>> Area Meteorologica e  Area nivologica - Centro Funzionale
>> Servizio Protezione Civile - Regione Marche
>> Via del Colle Ameno 5
>> 60126 Torrette di Ancona, Ancona
>> Uff: 071 806 7743
>> E-mail: [hidden email]
>> ---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 Libra ESVA ed  risultato non
>infetto.
>> This message was scanned by Libra ESVA and is believed to be clean.
>>
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] 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.
>
>______________________________________________
>[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Sum of columns of a data frame equal to NA when all the elements are NA

Boris Steipe
Surely the result of summation of non-existent values is not defined, is it not? And since the NA values have been _removed_, there's nothing left to sum over. In fact, pretending the the result in that case is zero would appear audacious, no?

Cheers,
Boris





> On Mar 21, 2018, at 12:58 PM, Jeff Newmiller <[hidden email]> wrote:
>
> What do you mean by "should not"?
>
> NULL means "missing object" in R. The result of the sum function is always expected to be numeric... so NA_real or NA_integer could make sense as possible return values. But you cannot compute on NULL so no, that doesn't work.
>
> See the note under the "Value" section of ?sum as to why zero is returned when all inputs are removed.
> --
> Sent from my phone. Please excuse my brevity.
>
> On March 21, 2018 9:03:29 AM PDT, Boris Steipe <[hidden email]> wrote:
>> Should not the result be NULL if you have removed the NA with
>> na.rm=TRUE ?
>>
>> B.
>>
>>
>>
>>> On Mar 21, 2018, at 11:44 AM, Stefano Sofia
>> <[hidden email]> wrote:
>>>
>>> Dear list users,
>>> let me ask you this trivial question. I worked on that for a long
>> time, by now.
>>> Suppose to have a data frame with NAs and to sum some columns with
>> rowSums:
>>>
>>> df <- data.frame(A = runif(10), B = runif(10), C = rnorm(10))
>>> df[1, ] <- NA
>>> rowSums(df[ , which(names(df) %in% c("A","B"))], na.rm=T)
>>>
>>> If all the elements of the selected columns are NA, rowSums returns 0
>> while I need NA.
>>> Is there an easy and efficient way to use rowSums within a function
>> like
>>>
>>> function(x) ifelse(all(is.na(x)), as.numeric(NA), rowSums...)?
>>>
>>> or an equivalent function?
>>>
>>> Thank you for your help
>>> Stefano
>>>
>>>
>>>
>>>        (oo)
>>> --oOO--( )--OOo----------------
>>> Stefano Sofia PhD
>>> Area Meteorologica e  Area nivologica - Centro Funzionale
>>> Servizio Protezione Civile - Regione Marche
>>> Via del Colle Ameno 5
>>> 60126 Torrette di Ancona, Ancona
>>> Uff: 071 806 7743
>>> E-mail: [hidden email]
>>> ---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 Libra ESVA ed  risultato non
>> infetto.
>>> This message was scanned by Libra ESVA and is believed to be clean.
>>>
>>>
>>> [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> [hidden email] 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.
>>
>> ______________________________________________
>> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Sum of columns of a data frame equal to NA when all the elements are NA

Peter Dalgaard-2
No. The empty sum is zero. Adding it to another sum should not change it. Nothing audacious about that. This is consistent; other definitions just cause trouble.

-pd

> On 21 Mar 2018, at 18:05 , Boris Steipe <[hidden email]> wrote:
>
> Surely the result of summation of non-existent values is not defined, is it not? And since the NA values have been _removed_, there's nothing left to sum over. In fact, pretending the the result in that case is zero would appear audacious, no?
>
> Cheers,
> Boris
>
>
>
>
>
>> On Mar 21, 2018, at 12:58 PM, Jeff Newmiller <[hidden email]> wrote:
>>
>> What do you mean by "should not"?
>>
>> NULL means "missing object" in R. The result of the sum function is always expected to be numeric... so NA_real or NA_integer could make sense as possible return values. But you cannot compute on NULL so no, that doesn't work.
>>
>> See the note under the "Value" section of ?sum as to why zero is returned when all inputs are removed.
>> --
>> Sent from my phone. Please excuse my brevity.
>>
>> On March 21, 2018 9:03:29 AM PDT, Boris Steipe <[hidden email]> wrote:
>>> Should not the result be NULL if you have removed the NA with
>>> na.rm=TRUE ?
>>>
>>> B.
>>>
>>>
>>>
>>>> On Mar 21, 2018, at 11:44 AM, Stefano Sofia
>>> <[hidden email]> wrote:
>>>>
>>>> Dear list users,
>>>> let me ask you this trivial question. I worked on that for a long
>>> time, by now.
>>>> Suppose to have a data frame with NAs and to sum some columns with
>>> rowSums:
>>>>
>>>> df <- data.frame(A = runif(10), B = runif(10), C = rnorm(10))
>>>> df[1, ] <- NA
>>>> rowSums(df[ , which(names(df) %in% c("A","B"))], na.rm=T)
>>>>
>>>> If all the elements of the selected columns are NA, rowSums returns 0
>>> while I need NA.
>>>> Is there an easy and efficient way to use rowSums within a function
>>> like
>>>>
>>>> function(x) ifelse(all(is.na(x)), as.numeric(NA), rowSums...)?
>>>>
>>>> or an equivalent function?
>>>>
>>>> Thank you for your help
>>>> Stefano
>>>>
>>>>
>>>>
>>>>       (oo)
>>>> --oOO--( )--OOo----------------
>>>> Stefano Sofia PhD
>>>> Area Meteorologica e  Area nivologica - Centro Funzionale
>>>> Servizio Protezione Civile - Regione Marche
>>>> Via del Colle Ameno 5
>>>> 60126 Torrette di Ancona, Ancona
>>>> Uff: 071 806 7743
>>>> E-mail: [hidden email]
>>>> ---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 Libra ESVA ed  risultato non
>>> infetto.
>>>> This message was scanned by Libra ESVA and is believed to be clean.
>>>>
>>>>
>>>> [[alternative HTML version deleted]]
>>>>
>>>> ______________________________________________
>>>> [hidden email] 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.
>>>
>>> ______________________________________________
>>> [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.

--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: [hidden email]  Priv: [hidden email]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Sum of columns of a data frame equal to NA when all the elements are NA

Boris Steipe
I see: consistency with additive identity. That makes sense. Thanks.

B.


> On Mar 21, 2018, at 1:22 PM, peter dalgaard <[hidden email]> wrote:
>
> No. The empty sum is zero. Adding it to another sum should not change it. Nothing audacious about that. This is consistent; other definitions just cause trouble.
>
> -pd
>
>> On 21 Mar 2018, at 18:05 , Boris Steipe <[hidden email]> wrote:
>>
>> Surely the result of summation of non-existent values is not defined, is it not? And since the NA values have been _removed_, there's nothing left to sum over. In fact, pretending the the result in that case is zero would appear audacious, no?
>>
>> Cheers,
>> Boris
>>
>>
>>
>>
>>
>>> On Mar 21, 2018, at 12:58 PM, Jeff Newmiller <[hidden email]> wrote:
>>>
>>> What do you mean by "should not"?
>>>
>>> NULL means "missing object" in R. The result of the sum function is always expected to be numeric... so NA_real or NA_integer could make sense as possible return values. But you cannot compute on NULL so no, that doesn't work.
>>>
>>> See the note under the "Value" section of ?sum as to why zero is returned when all inputs are removed.
>>> --
>>> Sent from my phone. Please excuse my brevity.
>>>
>>> On March 21, 2018 9:03:29 AM PDT, Boris Steipe <[hidden email]> wrote:
>>>> Should not the result be NULL if you have removed the NA with
>>>> na.rm=TRUE ?
>>>>
>>>> B.
>>>>
>>>>
>>>>
>>>>> On Mar 21, 2018, at 11:44 AM, Stefano Sofia
>>>> <[hidden email]> wrote:
>>>>>
>>>>> Dear list users,
>>>>> let me ask you this trivial question. I worked on that for a long
>>>> time, by now.
>>>>> Suppose to have a data frame with NAs and to sum some columns with
>>>> rowSums:
>>>>>
>>>>> df <- data.frame(A = runif(10), B = runif(10), C = rnorm(10))
>>>>> df[1, ] <- NA
>>>>> rowSums(df[ , which(names(df) %in% c("A","B"))], na.rm=T)
>>>>>
>>>>> If all the elements of the selected columns are NA, rowSums returns 0
>>>> while I need NA.
>>>>> Is there an easy and efficient way to use rowSums within a function
>>>> like
>>>>>
>>>>> function(x) ifelse(all(is.na(x)), as.numeric(NA), rowSums...)?
>>>>>
>>>>> or an equivalent function?
>>>>>
>>>>> Thank you for your help
>>>>> Stefano
>>>>>
>>>>>
>>>>>
>>>>>      (oo)
>>>>> --oOO--( )--OOo----------------
>>>>> Stefano Sofia PhD
>>>>> Area Meteorologica e  Area nivologica - Centro Funzionale
>>>>> Servizio Protezione Civile - Regione Marche
>>>>> Via del Colle Ameno 5
>>>>> 60126 Torrette di Ancona, Ancona
>>>>> Uff: 071 806 7743
>>>>> E-mail: [hidden email]
>>>>> ---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 Libra ESVA ed  risultato non
>>>> infetto.
>>>>> This message was scanned by Libra ESVA and is believed to be clean.
>>>>>
>>>>>
>>>>> [[alternative HTML version deleted]]
>>>>>
>>>>> ______________________________________________
>>>>> [hidden email] 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.
>>>>
>>>> ______________________________________________
>>>> [hidden email] 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.
>>
>> ______________________________________________
>> [hidden email] 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.
>
> --
> Peter Dalgaard, Professor,
> Center for Statistics, Copenhagen Business School
> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
> Phone: (+45)38153501
> Office: A 4.23
> Email: [hidden email]  Priv: [hidden email]
>
>
>
>
>
>
>
>
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Sum of columns of a data frame equal to NA when all the elements are NA

R help mailing list-2
In reply to this post by Stefano Sofia
Use rowSums(is.na(d))==ncol(d) to pick out the rows will no good values.
E.g.,

> d <- data.frame(x1=c(NA,2:4), x2=c(NA,NA,13:14), x3=c(NA,NA,NA,44))
> d
  x1 x2 x3
1 NA NA NA
2  2 NA NA
3  3 13 NA
4  4 14 44
> s <- rowSums(d, na.rm=TRUE)
> s
[1]  0  2 16 62
> s[ rowSums(is.na(d))==ncol(d) ] <- NA
> s
[1] NA  2 16 62


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Wed, Mar 21, 2018 at 8:44 AM, Stefano Sofia <
[hidden email]> wrote:

> Dear list users,
> let me ask you this trivial question. I worked on that for a long time, by
> now.
> Suppose to have a data frame with NAs and to sum some columns with rowSums:
>
> df <- data.frame(A = runif(10), B = runif(10), C = rnorm(10))
> df[1, ] <- NA
> rowSums(df[ , which(names(df) %in% c("A","B"))], na.rm=T)
>
> If all the elements of the selected columns are NA, rowSums returns 0
> while I need NA.
> Is there an easy and efficient way to use rowSums within a function like
>
> function(x) ifelse(all(is.na(x)), as.numeric(NA), rowSums...)?
>
> or an equivalent function?
>
> Thank you for your help
> Stefano
>
>
>
>          (oo)
> --oOO--( )--OOo----------------
> Stefano Sofia PhD
> Area Meteorologica e  Area nivologica - Centro Funzionale
> Servizio Protezione Civile - Regione Marche
> Via del Colle Ameno 5
> 60126 Torrette di Ancona, Ancona
> Uff: 071 806 7743
> E-mail: [hidden email]
> ---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 Libra ESVA ed  risultato non infetto.
> This message was scanned by Libra ESVA and is believed to be clean.
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Sum of columns of a data frame equal to NA when all the elements are NA

David Winsemius
In reply to this post by Jeff Newmiller

> On Mar 21, 2018, at 9:58 AM, Jeff Newmiller <[hidden email]> wrote:
>
> What do you mean by "should not"?
>
> NULL means "missing object" in R. The result of the sum function is always expected to be numeric... so NA_real or NA_integer could make sense as possible return values. But you cannot compute on NULL so no, that doesn't work.
>
> See the note under the "Value" section of ?sum as to why zero is returned when all inputs are removed.

But your perspective above that sentence is at odds with the note in the Details section of the same document:

> sum(NULL)
[1] 0
> ?sum
starting httpd help server ... done
> sum( integer(0) )
[1] 0

Best;
David

> --
> Sent from my phone. Please excuse my brevity.
>
> On March 21, 2018 9:03:29 AM PDT, Boris Steipe <[hidden email]> wrote:
>> Should not the result be NULL if you have removed the NA with
>> na.rm=TRUE ?
>>
>> B.
>>
>>
>>
>>> On Mar 21, 2018, at 11:44 AM, Stefano Sofia
>> <[hidden email]> wrote:
>>>
>>> Dear list users,
>>> let me ask you this trivial question. I worked on that for a long
>> time, by now.
>>> Suppose to have a data frame with NAs and to sum some columns with
>> rowSums:
>>>
>>> df <- data.frame(A = runif(10), B = runif(10), C = rnorm(10))
>>> df[1, ] <- NA
>>> rowSums(df[ , which(names(df) %in% c("A","B"))], na.rm=T)
>>>
>>> If all the elements of the selected columns are NA, rowSums returns 0
>> while I need NA.
>>> Is there an easy and efficient way to use rowSums within a function
>> like
>>>
>>> function(x) ifelse(all(is.na(x)), as.numeric(NA), rowSums...)?
>>>
>>> or an equivalent function?
>>>
>>> Thank you for your help
>>> Stefano
>>>
>>>
>>>
>>>        (oo)
>>> --oOO--( )--OOo----------------
>>> Stefano Sofia PhD
>>> Area Meteorologica e  Area nivologica - Centro Funzionale
>>> Servizio Protezione Civile - Regione Marche
>>> Via del Colle Ameno 5
>>> 60126 Torrette di Ancona, Ancona
>>> Uff: 071 806 7743
>>> E-mail: [hidden email]
>>> ---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 Libra ESVA ed  risultato non
>> infetto.
>>> This message was scanned by Libra ESVA and is believed to be clean.
>>>
>>>
>>> [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> [hidden email] 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.
>>
>> ______________________________________________
>> [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.

David Winsemius
Alameda, CA, USA

'Any technology distinguishable from magic is insufficiently advanced.'   -Gehm's Corollary to Clarke's Third Law

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Sum of columns of a data frame equal to NA when all the elements are NA

Bert Gunter-2
In reply to this post by Boris Steipe
"I see: consistency with additive identity. "

Ummm, well:

> 1+NULL
numeric(0)

> sum(1,NULL)
[1] 1

Of course, there could well be something here I don't get, but that doesn't
look very consistent to me. However, as I said privately, so long as the
corner case behavior is documented, which it is, I don't care.

Cheers,
Bert



Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Wed, Mar 21, 2018 at 10:26 AM, Boris Steipe <[hidden email]>
wrote:

> I see: consistency with additive identity. That makes sense. Thanks.
>
> B.
>
>
> > On Mar 21, 2018, at 1:22 PM, peter dalgaard <[hidden email]> wrote:
> >
> > No. The empty sum is zero. Adding it to another sum should not change
> it. Nothing audacious about that. This is consistent; other definitions
> just cause trouble.
> >
> > -pd
> >
> >> On 21 Mar 2018, at 18:05 , Boris Steipe <[hidden email]>
> wrote:
> >>
> >> Surely the result of summation of non-existent values is not defined,
> is it not? And since the NA values have been _removed_, there's nothing
> left to sum over. In fact, pretending the the result in that case is zero
> would appear audacious, no?
> >>
> >> Cheers,
> >> Boris
> >>
> >>
> >>
> >>
> >>
> >>> On Mar 21, 2018, at 12:58 PM, Jeff Newmiller <[hidden email]>
> wrote:
> >>>
> >>> What do you mean by "should not"?
> >>>
> >>> NULL means "missing object" in R. The result of the sum function is
> always expected to be numeric... so NA_real or NA_integer could make sense
> as possible return values. But you cannot compute on NULL so no, that
> doesn't work.
> >>>
> >>> See the note under the "Value" section of ?sum as to why zero is
> returned when all inputs are removed.
> >>> --
> >>> Sent from my phone. Please excuse my brevity.
> >>>
> >>> On March 21, 2018 9:03:29 AM PDT, Boris Steipe <
> [hidden email]> wrote:
> >>>> Should not the result be NULL if you have removed the NA with
> >>>> na.rm=TRUE ?
> >>>>
> >>>> B.
> >>>>
> >>>>
> >>>>
> >>>>> On Mar 21, 2018, at 11:44 AM, Stefano Sofia
> >>>> <[hidden email]> wrote:
> >>>>>
> >>>>> Dear list users,
> >>>>> let me ask you this trivial question. I worked on that for a long
> >>>> time, by now.
> >>>>> Suppose to have a data frame with NAs and to sum some columns with
> >>>> rowSums:
> >>>>>
> >>>>> df <- data.frame(A = runif(10), B = runif(10), C = rnorm(10))
> >>>>> df[1, ] <- NA
> >>>>> rowSums(df[ , which(names(df) %in% c("A","B"))], na.rm=T)
> >>>>>
> >>>>> If all the elements of the selected columns are NA, rowSums returns 0
> >>>> while I need NA.
> >>>>> Is there an easy and efficient way to use rowSums within a function
> >>>> like
> >>>>>
> >>>>> function(x) ifelse(all(is.na(x)), as.numeric(NA), rowSums...)?
> >>>>>
> >>>>> or an equivalent function?
> >>>>>
> >>>>> Thank you for your help
> >>>>> Stefano
> >>>>>
> >>>>>
> >>>>>
> >>>>>      (oo)
> >>>>> --oOO--( )--OOo----------------
> >>>>> Stefano Sofia PhD
> >>>>> Area Meteorologica e  Area nivologica - Centro Funzionale
> >>>>> Servizio Protezione Civile - Regione Marche
> >>>>> Via del Colle Ameno 5
> >>>>> 60126 Torrette di Ancona, Ancona
> >>>>> Uff: 071 806 7743
> >>>>> E-mail: [hidden email]
> >>>>> ---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 Libra ESVA ed  risultato non
> >>>> infetto.
> >>>>> This message was scanned by Libra ESVA and is believed to be clean.
> >>>>>
> >>>>>
> >>>>>   [[alternative HTML version deleted]]
> >>>>>
> >>>>> ______________________________________________
> >>>>> [hidden email] 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.
> >>>>
> >>>> ______________________________________________
> >>>> [hidden email] 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.
> >>
> >> ______________________________________________
> >> [hidden email] 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.
> >
> > --
> > Peter Dalgaard, Professor,
> > Center for Statistics, Copenhagen Business School
> > Solbjerg Plads 3, 2000 Frederiksberg, Denmark
> > Phone: (+45)38153501
> > Office: A 4.23
> > Email: [hidden email]  Priv: [hidden email]
> >
> >
> >
> >
> >
> >
> >
> >
> >
>
> ______________________________________________
> [hidden email] 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Sum of columns of a data frame equal to NA when all the elements are NA

Stefano Sofia
In reply to this post by Duncan Murdoch-2
Thank you.
Perfect solution.

Stefano

         (oo)
--oOO--( )--OOo----------------
Stefano Sofia PhD
Area Meteorologica e  Area nivologica - Centro Funzionale
Servizio Protezione Civile - Regione Marche
Via del Colle Ameno 5
60126 Torrette di Ancona, Ancona
Uff: 071 806 7743
E-mail: [hidden email]
---Oo---------oO----------------
________________________________________
Da: Duncan Murdoch [[hidden email]]
Inviato: mercoledì 21 marzo 2018 17.01
A: Stefano Sofia; [hidden email]
Oggetto: Re: [R] Sum of columns of a data frame equal to NA when all the elements are NA

On 21/03/2018 11:44 AM, Stefano Sofia wrote:

> Dear list users,
> let me ask you this trivial question. I worked on that for a long time, by now.
> Suppose to have a data frame with NAs and to sum some columns with rowSums:
>
> df <- data.frame(A = runif(10), B = runif(10), C = rnorm(10))
> df[1, ] <- NA
> rowSums(df[ , which(names(df) %in% c("A","B"))], na.rm=T)
>
> If all the elements of the selected columns are NA, rowSums returns 0 while I need NA.
> Is there an easy and efficient way to use rowSums within a function like
>
> function(x) ifelse(all(is.na(x)), as.numeric(NA), rowSums...)?
>
> or an equivalent function?

Something like

apply(df[,c("A", "B")], 1, function(x) if (all(is.na(x))) NA else sum(x,
na.rm = TRUE))

should do what you want.

Duncan Murdoch

>
> Thank you for your help
> Stefano
>
>
>
>           (oo)
> --oOO--( )--OOo----------------
> Stefano Sofia PhD
> Area Meteorologica e  Area nivologica - Centro Funzionale
> Servizio Protezione Civile - Regione Marche
> Via del Colle Ameno 5
> 60126 Torrette di Ancona, Ancona
> Uff: 071 806 7743
> E-mail: [hidden email]
> ---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 Libra ESVA ed  risultato non infetto.
> This message was scanned by Libra ESVA and is believed to be clean.
>
>
>       [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>  https://urlsand.esvalabs.com/?u=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&e=52342f8a&h=d46bc785&f=y&p=y
> PLEASE do read the posting guide  https://urlsand.esvalabs.com/?u=http%3A%2F%2Fwww.R-project.org%2Fposting-guide.html&e=52342f8a&h=9b25bfd5&f=y&p=y
> and provide commented, minimal, self-contained, reproducible code.
>


--

Questo messaggio  stato analizzato con Libra ESVA ed  risultato non infetto.


________________________________

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 Libra ESVA ed  risultato non infetto.
This message was scanned by Libra ESVA and is believed to be clean.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Sum of columns of a data frame equal to NA when all the elements are NA

Jeff Newmiller
In reply to this post by Bert Gunter-2
I can see that one might regard having

sum( sum( 1 ), sum( NULL ) ) == sum( 1 )

be TRUE as a necessary consistency, but going down that road one might expect Bert's

v+NULL == v

for all numeric vectors also. I have always avoided that construction as poor computing practice, but if NULL is supposed to represent the empty set mathematically [1] then this would seem to follow.

[1] https://cran.r-project.org/doc/contrib/de_Jonge+van_der_Loo-Introduction_to_data_cleaning_with_R.pdf

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

On March 21, 2018 1:06:46 PM PDT, Bert Gunter <[hidden email]> wrote:

>"I see: consistency with additive identity. "
>
>Ummm, well:
>
>> 1+NULL
>numeric(0)
>
>> sum(1,NULL)
>[1] 1
>
>Of course, there could well be something here I don't get, but that
>doesn't
>look very consistent to me. However, as I said privately, so long as
>the
>corner case behavior is documented, which it is, I don't care.
>
>Cheers,
>Bert
>
>
>
>Bert Gunter
>
>"The trouble with having an open mind is that people keep coming along
>and
>sticking things into it."
>-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>On Wed, Mar 21, 2018 at 10:26 AM, Boris Steipe
><[hidden email]>
>wrote:
>
>> I see: consistency with additive identity. That makes sense. Thanks.
>>
>> B.
>>
>>
>> > On Mar 21, 2018, at 1:22 PM, peter dalgaard <[hidden email]>
>wrote:
>> >
>> > No. The empty sum is zero. Adding it to another sum should not
>change
>> it. Nothing audacious about that. This is consistent; other
>definitions
>> just cause trouble.
>> >
>> > -pd
>> >
>> >> On 21 Mar 2018, at 18:05 , Boris Steipe <[hidden email]>
>> wrote:
>> >>
>> >> Surely the result of summation of non-existent values is not
>defined,
>> is it not? And since the NA values have been _removed_, there's
>nothing
>> left to sum over. In fact, pretending the the result in that case is
>zero
>> would appear audacious, no?
>> >>
>> >> Cheers,
>> >> Boris
>> >>
>> >>
>> >>
>> >>
>> >>
>> >>> On Mar 21, 2018, at 12:58 PM, Jeff Newmiller
><[hidden email]>
>> wrote:
>> >>>
>> >>> What do you mean by "should not"?
>> >>>
>> >>> NULL means "missing object" in R. The result of the sum function
>is
>> always expected to be numeric... so NA_real or NA_integer could make
>sense
>> as possible return values. But you cannot compute on NULL so no, that
>> doesn't work.
>> >>>
>> >>> See the note under the "Value" section of ?sum as to why zero is
>> returned when all inputs are removed.
>> >>> --
>> >>> Sent from my phone. Please excuse my brevity.
>> >>>
>> >>> On March 21, 2018 9:03:29 AM PDT, Boris Steipe <
>> [hidden email]> wrote:
>> >>>> Should not the result be NULL if you have removed the NA with
>> >>>> na.rm=TRUE ?
>> >>>>
>> >>>> B.
>> >>>>
>> >>>>
>> >>>>
>> >>>>> On Mar 21, 2018, at 11:44 AM, Stefano Sofia
>> >>>> <[hidden email]> wrote:
>> >>>>>
>> >>>>> Dear list users,
>> >>>>> let me ask you this trivial question. I worked on that for a
>long
>> >>>> time, by now.
>> >>>>> Suppose to have a data frame with NAs and to sum some columns
>with
>> >>>> rowSums:
>> >>>>>
>> >>>>> df <- data.frame(A = runif(10), B = runif(10), C = rnorm(10))
>> >>>>> df[1, ] <- NA
>> >>>>> rowSums(df[ , which(names(df) %in% c("A","B"))], na.rm=T)
>> >>>>>
>> >>>>> If all the elements of the selected columns are NA, rowSums
>returns 0
>> >>>> while I need NA.
>> >>>>> Is there an easy and efficient way to use rowSums within a
>function
>> >>>> like
>> >>>>>
>> >>>>> function(x) ifelse(all(is.na(x)), as.numeric(NA), rowSums...)?
>> >>>>>
>> >>>>> or an equivalent function?
>> >>>>>
>> >>>>> Thank you for your help
>> >>>>> Stefano
>> >>>>>
>> >>>>>
>> >>>>>
>> >>>>>      (oo)
>> >>>>> --oOO--( )--OOo----------------
>> >>>>> Stefano Sofia PhD
>> >>>>> Area Meteorologica e  Area nivologica - Centro Funzionale
>> >>>>> Servizio Protezione Civile - Regione Marche
>> >>>>> Via del Colle Ameno 5
>> >>>>> 60126 Torrette di Ancona, Ancona
>> >>>>> Uff: 071 806 7743
>> >>>>> E-mail: [hidden email]
>> >>>>> ---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 Libra ESVA ed  risultato
>non
>> >>>> infetto.
>> >>>>> This message was scanned by Libra ESVA and is believed to be
>clean.
>> >>>>>
>> >>>>>
>> >>>>>   [[alternative HTML version deleted]]
>> >>>>>
>> >>>>> ______________________________________________
>> >>>>> [hidden email] 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.
>> >>>>
>> >>>> ______________________________________________
>> >>>> [hidden email] 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.
>> >>
>> >> ______________________________________________
>> >> [hidden email] 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.
>> >
>> > --
>> > Peter Dalgaard, Professor,
>> > Center for Statistics, Copenhagen Business School
>> > Solbjerg Plads 3, 2000 Frederiksberg, Denmark
>> > Phone: (+45)38153501
>> > Office: A 4.23
>> > Email: [hidden email]  Priv: [hidden email]
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>>
>> ______________________________________________
>> [hidden email] 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.
>>

______________________________________________
[hidden email] 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.