I have the following data frame.
set.seed(2)
va<-data.frame(x=rep(NA, 10),
y=sample(c(NA,2,3),10,T))
va
x y
1 NA NA
2 NA 3
3 NA 2
4 NA 2
5 NA NA
6 NA NA
7 NA NA
8 NA NA
9 NA NA
10 NA 2
And when adding by columns I use the function rowSums()
.
rowSums(va[,1:2], na.rm = T)
[1] 0 3 2 2 0 0 0 0 0 2
But I get that when adding NA
with NA
it gives me zero. NA
with should NA
n't it be NA
?
What is this about? Is there another function that when adding two or more columns does not give me zero as a result?
With
na.rm = T
you are telling it to ignore theNA
. If you indicatena.rm = F
or do not indicate anything (by default it is initialized toFALSE
), it will give you the resultNA
.More information can be found in the documentation here .
I add the following code in case you want to add in case there is a
NA
in one column and a number in another.The value
NA
has the characteristic that transforms any sum into NA regardless of the data type of its addend:In this sense, the functions like
rowSums()
and many others have two treatments with respect to theNA
, consider it or ignore it (na.rm = T
) and particularlyrowSums()
document the behavior of your example:That is, when the two values are
NA
the return will be 0. So if what you are looking for is that in these cases the value is,NA
you will have to do something different:Addendum
The behavior
rowSums()
is consistent with that of simplesum
, wheresum(NA, na.rm=TRUE) == 0
and going furthersum() == 0
and has to do with a certain, albeit provable, convention for what is known as empty sum .