[R] binary operators that never return missing values

John Kane jrkrideau at inbox.com
Wed Jun 20 22:52:50 CEST 2012


I'm not sure I got the question but  is this something like what you want?
      x[is.na(x) ] <-  "FALSE"
   x

John Kane
Kingston ON Canada


> -----Original Message-----
> From: ajdamico at gmail.com
> Sent: Wed, 20 Jun 2012 16:44:25 -0400
> To: r-help at r-project.org
> Subject: [R] binary operators that never return missing values
> 
> Hi, I work with data sets with lots of missing values.  We often need
> to conduct logical tests on numeric vectors containing missing values.
>  I've searched around for material and conversations on this topic,
> but I'm having a hard time finding anything.  Has anyone written a
> package that deals with this sort of thing?  All I want are a group of
> functions like the ones I've posted below, but I'm worried I'm
> re-inventing the wheel..  If they're not already on CRAN, I feel like
> I should add them.  Any pointers to work already completed on this
> subject would be appreciated.  Thanks!
> 
> Anthony Damico
> Kaiser Family Foundation
> 
> 
> 
> Here's a simple example of what I need done on a regular basis:
> 
> #two numeric vectors
> a <- c( 1 , NA , 7 , 2 , NA )
> 
> b <- c( NA , NA , 9 , 1 , 6 )
> 
> #this has lots of NAs
> a > b
> 
> #save this result in x
> x <- (a > b)
> 
> #overwrite NAs in x with falses (which we do a lot)
> x <- ifelse( is.na( x ) , F , x )
> 
> #now x has only trues and falses
> x
> 
> 
> 
> ################
> Here's an example function that solves the problem for "greater than"
> ################
> 
> 
> #construct a function that performs the same steps:
> "%>F%" <-
> 	function( a , b ){
> 
> 		x <- (a > b)
> 
> 		x.false <- ifelse( is.na( x ) , F , x )
> 
> 		x.false
> 
> 	}
> 
> #run the function
> 
> a %>F% b
> 
> ______________________________________________
> R-help at 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.

____________________________________________________________
GET FREE SMILEYS FOR YOUR IM & EMAIL - Learn more at http://www.inbox.com/smileys
Works with AIM®, MSN® Messenger, Yahoo!® Messenger, ICQ®, Google Talk™ and most webmails



More information about the R-help mailing list