On Jun 20, 2012, at 5:18 PM, Anthony Damico wrote:

Thanks Michael,

I was hoping to complete this in one step (since I use these a lot).
Setting the class of the vectors seems like more typing than just
doing %>F%  ...

Unfortunately, my knowledge of classes, methods, and the like is
pretty shaky.  Is it possible to *create* another set of operators
that just changes NAs to falses or trues?--

F  <F  >=F  <=F  >T  <T  >=T  <=T

There is the capacity to define new infix operators:

?`%in%    # which also never returns NA

> a <- c( 1 , NA , 7 , 2 , NA )
> b <- c( NA , NA , 9 , 1 , 6 )
>
> `%>%` <- function(x,y) ifelse( is.na(x)|is.na(y), FALSE, x > y)
> a %>% b
[1] FALSE FALSE FALSE  TRUE FALSE
> `%<%` <- function(x,y) ifelse( is.na(x)|is.na(y), FALSE, x < y)
> a %<% b
[1] FALSE FALSE  TRUE FALSE FALSE

-- David.

I definitely don't want to change the base functionality of the
operators, so infix notation seems like the strategy that will require
the least typing in the long run..  but my mind is still open. :)



On Wed, Jun 20, 2012 at 4:54 PM, R. Michael Weylandt
<michael.weyla...@gmail.com> wrote:
Hi Anthony,

No, I don't believe this exists on CRAN already (happy to be proven
wrong though) but I might suggest you approach things a different way: instead of defining this operator by operator with infix notation, why
not go after `+`, `>` directly? If you put a class on your vectors,
you can define Ops.class which will change the behavior of all those
sorts of things.

Simple example (probably not complete nor necessarily advisable)

a <- c( 1 , NA , 7 , 2 , NA )
b <- c( NA , NA , 9 , 1 , 6 )

class(a) <- class(b) <- "damico"

Ops.damico <- function(e1, e2 = NULL){
   e1[is.na(e1)] <- 0
   e2[is.na(e2)] <- 0
   NextMethod()
}

a < b

More nuance is available, but this hopefully gives you a start. You
might, e.g., think about setting this as something more like:

Ops.damico <- function(e1, e2 = NULL){
   if(.Generic %in% c("==","!=","<","<=",">=",">")){
       e1[is.na(e1)] <- 0
       e2[is.na(e2)] <- 0
   }
   NextMethod()
}

so you don't mess up arithmetic but only the boolean comparisons.

Best,
Michael

On Wed, Jun 20, 2012 at 3:44 PM, Anthony Damico <ajdam...@gmail.com> wrote:
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@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.

______________________________________________
R-help@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.

David Winsemius, MD
West Hartford, CT

______________________________________________
R-help@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.

Reply via email to