Skip to content

Handling NA values

6 messages · Marc Schwartz, Berend Hasselman, Christofer Bogaso +1 more

#
Hello again,

I have a question on who sum() handle the NA values.
[1] 1


I understand this. However could not agree with following:
[1] 0


Where this '0' is coming from? Should not it be NA itself?

Thanks and regards,
#
On Feb 16, 2013, at 11:55 AM, Christofer Bogaso <bogaso.christofer at gmail.com> wrote:

            
The result of:

  sum(c(NA, NA), na.rm = TRUE)

is to sum an empty set, hence the 0.
logical(0)
attr(,"na.action")
[1] 1 2
attr(,"class")
[1] "omit"
[1] 0


If you retained the NA's, then the result is undefined:
[1] NA


See:

  http://rwiki.sciviews.org/doku.php?id=tips:surprises:emptysetfuncs

for more information.

Regards,

Marc Schwartz
#
On 16-02-2013, at 18:55, Christofer Bogaso <bogaso.christofer at gmail.com> wrote:

            
sum(c())
[1] 0
is equivalent to sum(c()) or sum(NULL) or sum(integer(0).

See the NB in the value section of the help page for sum.

Berend
#
Thanks Marc for your reply.

However this leads to my problem of handling rowSums() function (hence
colSums()). Let take following matrix:
[,1] [,2] [,3]
[1,]    1   -1   NA
[2,]    1    1    1
[3,]   NA   NA   NA
[1] 0 3 0

I want to have some way to distinguish the 1st '0' and the 3rd '0'. I
want to see NA directly for the 3rd. Any possibility how to do that
through the rowSum() function?

Thanks and regards,
On Sat, Feb 16, 2013 at 11:52 PM, Marc Schwartz <marc_schwartz at me.com> wrote:
#
Nothing comes to mind immediately for rowSums() or colSums() given the way in which they handle things, however using apply() you have more flexibility, albeit at the price of some speed:
[1]  0  3 NA

Essentially, if all elements in the row are NA's return NA otherwise return the sum of the non-NA elements.

Change the margin from 1 to 2 in apply() to use the same approach on columns.

Regards,

Marc
On Feb 16, 2013, at 12:22 PM, Christofer Bogaso <bogaso.christofer at gmail.com> wrote:

            
#
Hi,
Try this:
ifelse(rowSums(is.na(Mat))==ncol(Mat),NA,rowSums(Mat,na.rm=TRUE))
#[1]? 0? 3 NA
A.K.



----- Original Message -----
From: Christofer Bogaso <bogaso.christofer at gmail.com>
To: Marc Schwartz <marc_schwartz at me.com>
Cc: r-help <r-help at r-project.org>
Sent: Saturday, February 16, 2013 1:22 PM
Subject: Re: [R] Handling NA values

Thanks Marc for your reply.

However this leads to my problem of handling rowSums() function (hence
colSums()). Let take following matrix:
? ?  [,1] [,2] [,3]
[1,]? ? 1?  -1?  NA
[2,]? ? 1? ? 1? ? 1
[3,]?  NA?  NA?  NA
[1] 0 3 0

I want to have some way to distinguish the 1st '0' and the 3rd '0'. I
want to see NA directly for the 3rd. Any possibility how to do that
through the rowSum() function?

Thanks and regards,
On Sat, Feb 16, 2013 at 11:52 PM, Marc Schwartz <marc_schwartz at me.com> wrote:
______________________________________________
R-help at r-project.org mailing list
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.