[R] Adding across columns ignoring NA
gunter.berton at gene.com
Sat Jul 20 07:58:25 CEST 2013
No. The OP's "variables" appear to be vectors. sum() sums columnwise
appears to be what is wanted. But of course,Jin Choi appears to need
to do some homework -- e.g. by reading the Introduction to R online
manual to gain a better understanding of R's standard data structures.
On Fri, Jul 19, 2013 at 10:06 PM, Berend Hasselman <bhh at xs4all.nl> wrote:
> On 20-07-2013, at 06:37, Jin Choi <oohpsjin at gmail.com> wrote:
>> I am having difficulty finding a solution to devising an R code to do the
>> 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
>> 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
>> 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
> Have a look at the na.rm argument of sum.
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Genentech Nonclinical Biostatistics
Internal Contact Info:
More information about the R-help