HI,
 sum(var1,var2,var3,var4,var5,na.rm=TRUE)
#[1] 8
A.K.



----- Original Message -----
From: Jin Choi <oohps...@gmail.com>
To: "r-help@r-project.org" <r-help@r-project.org>
Cc: 
Sent: Saturday, July 20, 2013 12:37 AM
Subject: [R] Adding across columns ignoring NA

I am having difficulty finding a solution to devising an R code to do the
following:

I have 5 numerical variables and I would like to create a new variable that
is the sum of those 5 variables. However, there are many NA values
throughout these 5 variables and everytime I run the following code

new_variable=var1+var2+var3+var4+var5

I get NA as the sum whenever one of those 5 variables are NA. I cannot
figure out a way to have new_variable represent the sum for only those
values that are not NA.

As an example,
if var1=3
var2=3
var3=NA
var4=NA
var5=2

I would like new_variable to be 8 but I keep getting NA and I have
unsuccessfully tried different methods to do so. I feel there is a simple
method to solve my problem but I am unaware of such. I would appreciate any
guidance!

Thank you
JC

    [[alternative HTML version deleted]]

______________________________________________
R-help@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.


______________________________________________
R-help@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.

Reply via email to