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

Timothy Bates tim.bates at ed.ac.uk
Tue Sep 13 18:42:07 CEST 2011


Dear R cognoscenti,

While having NA as a native type is nifty, it is annoying when making binary choices.

Question: Is there anything bad about writing comparison functions that behavior like %in% (which I love) and ignore NAs?

"%>%" <- function(table, x) {
	return(which(table > x))
}

"%<%" <- function(table, x) {
	return(which(table < x))
}

test <- c(NA, 1:4,NA,5)
test %>% 2
# [1] 3 4 6
test %<% 2
# [1] 1

Why do I want to do this?

Because in coding, I often end up with big chunks looking like this:

((mydataframeName$myvariableName > 2 & !is.na(mydataframeName$myvariableName)) & (mydataframeName$myotherVariableName == "male" & !is.na(mydataframeName$myotherVariableName)))

Which is much less readable/maintainable/editable than

mydataframeName$myvariableName > 2 & mydataframeName$myotherVariableName == "male"

But ">" returns anything involving an NA, so it breaks selection statements (which can't contain NA) and leaves lines in data that are wished to be excluded

If this does not have nasty side-effects, it would be a great addition to GTD* in R

If anyone knows a short cut to code the effect I wish, love to hear it.

Cheers,
tim

* GTD = Getting Things Done


-- 
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.



More information about the R-help mailing list