[R] x %>% y as an alternative to which( x > y)

Timothy Bates timothy.c.bates at gmail.com
Wed Sep 14 14:57:56 CEST 2011


>> %<%  would extend the vocabulary established by %in%, and work in the same situations.
>> e.g. 
>> # only set “flynnEffect" for people known to be under 12, not for people where age is NA
>> twinData[twinData$Age %<% 12, "flynnEffect"] = FALSE 

>> Addressing Duncan's point about returning a logical array... the %<% function should be:
>> 
>> "%<%"<- function(table, x){
>> 	lessThan = table<  x
>> 	lessThan[is.na(lessThan)] = FALSE
>> 	return(lessThan)
>> }
> 
> I think that still doesn't work quite right.  You want the conversion of NA to FALSE to happen as the last
> part of evaluating an expression, not in intermediate steps.  Otherwise:
>        !(a %<% 10)
>  will give TRUE for NA values, which may not be as intended, if your intention was to skip NA cases.

Yes, so that would be by design: just as  !(1 %in% NA) returns TRUE, whereas  !(1 == NA).



More information about the R-help mailing list