Re: [R] Adding elements in an array where I have missing data.

From: John Kane <jrkrideau_at_yahoo.ca>
Date: Tue 02 May 2006 - 21:46:50 EST

> Here are a few alternatives:
>
> replace(a, is.na(a), 0) + b
>
> ifelse(is.na(a), 0, a) + b
>
> mapply(sum, a, b, MoreArgs = list(na.rm = TRUE))
>

Excellent, thanks Gabor. I have gotten some great answers. I had been thinking of using an ifelse but nothing as sucinct as this and the mapply was way beyond me. I didn't even know enough to look it up.

> On 5/1/06, John Kane <jrkrideau@yahoo.ca> wrote:
> > This is a simple question but I cannot seem to
> find
> > the answer.
> > I have two vectors but with missing data and I
> want to
> > add them together with
> > the NA's being ignored.
> >
> > Clearly I need to get the NA ignored. na.action?
> >
> > I have done some searching and cannot get
> na.action to
> > help.
> > This must be a common enough issue that the answer
> is
> > staring me in the face
> > but I just don't see it.
> >
> > Simple example
> > a <- c(2, NA, 3)
> > b <- c(3,4, 5)
> >
> > What I want is
> > c <- a + b where
> > c is ( 5 , 4 ,8)
> >
> > However I get
> > c is (5,NA, 8)
> >
> > What am I missing? Or do I somehow need to recode
> the
> > NA's as missing?
> >
> > Thanks
> >
> > ______________________________________________
> > R-help@stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
> >
>



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Tue May 02 21:59:34 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 03 May 2006 - 00:09:59 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.