# [R] Handling NA values

Christofer Bogaso bogaso.christofer at gmail.com
Sat Feb 16 19:22:34 CET 2013

```Thanks Marc for your reply.

However this leads to my problem of handling rowSums() function (hence
colSums()). Let take following matrix:

> Mat <- matrix(c(1, 1, NA, -1, 1, NA, NA, 1, NA), nc = 3)
> Mat
[,1] [,2] [,3]
[1,]    1   -1   NA
[2,]    1    1    1
[3,]   NA   NA   NA
> rowSums(Mat, na.rm = TRUE)
[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:
>
> On Feb 16, 2013, at 11:55 AM, Christofer Bogaso <bogaso.christofer at gmail.com> wrote:
>
>> Hello again,
>>
>> I have a question on who sum() handle the NA values.
>>
>>> sum(c(NA, 1), na.rm = TRUE)
>> [1] 1
>>
>>
>> I understand this. However could not agree with following:
>>
>>> sum(c(NA, NA), na.rm = TRUE)
>> [1] 0
>>
>>
>> Where this '0' is coming from? Should not it be NA itself?
>>
>> Thanks and regards,
>
>
>
> The result of:
>
>   sum(c(NA, NA), na.rm = TRUE)
>
> is to sum an empty set, hence the 0.
>
>
>> na.omit(c(NA, NA))
> logical(0)
> attr(,"na.action")
> [1] 1 2
> attr(,"class")
> [1] "omit"
>
>
>> sum(logical(0))
> [1] 0
>
>
> If you retained the NA's, then the result is undefined:
>
>> sum(c(NA, NA))
> [1] NA
>
>
> See:
>
>   http://rwiki.sciviews.org/doku.php?id=tips:surprises:emptysetfuncs
>