Is it possible to skip NA values in "+" operator?

Viewed 5378

I want to calculate an equation in R. I don't want to use the function sum because it's returning 1 value. I want the full vector of values.

x = 1:10
y = c(21:29,NA)
x+y
 [1] 22 24 26 28 30 32 34 36 38 NA

x = 1:10
y = c(21:30)
x+y
 [1] 22 24 26 28 30 32 34 36 38 40

I don't want:

sum(x,y, na.rm = TRUE)
[1] 280

Which does not return a vector.

This is a toy example but I have a more complex equation using multiple vector of length 84647 elements.

Here is another example of what I mean:

x = 1:10
y = c(21:29,NA)
z = 11:20
a = c(NA,NA,NA,30:36)
5 +2*(x+y-50)/(x+y+z+a) 
 [1]       NA       NA       NA 4.388889 4.473684 4.550000 4.619048 4.681818 4.739130       NA
5 Answers
Related