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

John Kane jrkrideau at yahoo.ca
Mon May 1 22:12:33 CEST 2006


--- Dave Armstrong <davearmstrong.ps at gmail.com> wrote:

> Dear John,
> 
> You can also get this by doing the following:
> 
> > apply(rbind(a,b), 2, sum, na.rm=T)
> [1] 5 4 8
> 
> This way, you don't have to actually change the
> missing values to zero.
> Best,
> Dave.

Hey, very nice indeed.  Thanks. 

I had not even realised  I could use a function like
that. I'm a newbie and still thinking in SAS or Systat
style.

> 
> --
> Dave Armstrong
> University of Maryland
> Dept of Government and Politics
> 3140 Tydings Hall
> College Park, MD 20742
> Office: 2103L Cole Field House
> 
> On 5/1/06, John Fox <jfox at mcmaster.ca> wrote:
> >
> > Dear John,
> >
> > The behaviour of R is reasonable, since, in your
> example, 4 + NA should in
> > general be NA (i.e., missing). You can do what you
> want by changing NAs to
> > 0s:
> >
> > > a[is.na(a)] <- 0
> > > a + b
> > [1] 5 4 8
> >
> > I hope this helps,
> > John
> >
> > --------------------------------
> > John Fox
> > Department of Sociology
> > McMaster University
> > Hamilton, Ontario
> > Canada L8S 4M4
> > 905-525-9140x23604
> > http://socserv.mcmaster.ca/jfox
> > --------------------------------
> >
> > > -----Original Message-----
> > > From: r-help-bounces at stat.math.ethz.ch
> > > [mailto:r-help-bounces at stat.math.ethz.ch] On
> Behalf Of John Kane
> > > Sent: Monday, May 01, 2006 12:26 PM
> > > To: R R-help
> > > Subject: [R] Adding elements in an array where I
> have missing data.
> > >
> > > 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 at 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 at 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
> >
>




More information about the R-help mailing list