[R] Adding elements in an array where I have missing data.
John Kane
jrkrideau at yahoo.ca
Mon May 1 21:56:08 CEST 2006
--- Rick Bilonick <bilonickra at upmc.edu> wrote:
> On Mon, 2006-05-01 at 13:26 -0400, John Kane 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
>
> Your example shows that you are not ignoring the
> NA's but treating them
> as zeroes. This may or may not make sense depending
> on the application.
>
> Your comment "Or do I somehow need to recode the
> NA's as missing" is
> puzzling. NA denotes a missing value.
>
> If you insist on treating NA's as zeroes, then just
> replace the NA's
> with zeroes before adding:
>
> > a[is.na(a)] <- 0
>
> Rick B.
Thanks, that is exactly what I wanted to know (and
do). In this case it makes sense to treat the data as
0's but in the original database I don't want to do
so.
This solution will probably be obvious in Help now but
I could not see an easy way to do this. Help is
sometimes a bit cryptic when one is a new user of R.
Sorry about the recode statement. I had intended to
write "recode as zero". The fingers are faster than
the mind.
More information about the R-help
mailing list