Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Summing lots of Vectors; row-wise or elementwise, but ignoring NA values

Tags:

r

na

vector

sum

I am trying to create a new vector that is the sum of 35 other vectors. The problem is that there are lots of NA values, but for this particular use, I want to treat those as zeros. Adding the vectors won't work, because if any of the 35 vectors contain an NA, the result is NA. Here is the example of the problem:

col1<-c(NA,1,2,3)
col2<-c(1,2,3,NA)
col3<-c(NA,NA,2,3)
Sum<-col1+col2+col3
Sum
# [1] NA NA  7 NA

I want the result to be 1, 3, 7, 6.
I suppose I could create new versions of each of the vectors in which I replace the NA with a 0, but that would be a lot of work when applied to 35 vectors. Is there a simple function that will help me out?

like image 397
user2980491 Avatar asked Nov 20 '13 19:11

user2980491


People also ask

How do you get rid of Na in a vector?

We can remove those NA values from the vector by using is.na(). is.na() is used to get the na values based on the vector index. ! is.na() will get the values except na.

How do I sum everything in a vector in R?

R – Sum of Vector Elements To find the sum of vector elements in R, call sum() function and pass the vector as argument. sum() returns a new vector with the elements of first vector appended with that of second vector.

Can you sum a vector?

The sum of two or more vectors is called the resultant. The resultant of two vectors can be found using either the parallelogram method or the triangle method .


2 Answers

Could also have used the rowSums function:

rowSums( cbind (col1,col2,col3), na.rm=TRUE)
#[1] 1 3 7 6

?rowSums   # also has colSums described on same help page
like image 165
IRTFM Avatar answered Nov 21 '22 20:11

IRTFM


Put them in a matrix first:

apply(cbind(col1,col2,col3),1,sum,na.rm = TRUE)
[1] 1 3 7 6

You can read about each function here using R's built-in documentation: ?apply, ?cbind.

cbind stands for "column bind": it takes several vectors or arrays and binds them "by column" into a single array:

cbind(col1,col2,col3)
     col1 col2 col3
[1,]   NA    1   NA
[2,]    1    2   NA
[3,]    2    3    2
[4,]    3   NA    3

apply, well, applies a function (sum in this case) to either the rows or columns of a matrix. This allows us to use the na.rm = TRUE argument to sum so that the NA values are dropped.

like image 29
joran Avatar answered Nov 21 '22 18:11

joran