[R] x %>% y as an alternative to which( x > y)
Greg Snow
Greg.Snow at imail.org
Thu Sep 15 17:19:51 CEST 2011
One additional thing to note is that %>% will have different precedence than > (something that was pointed out to me based on %<% that is in the TeachingDemos package).
--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Timothy Bates
> Sent: Tuesday, September 13, 2011 10:42 AM
> To: R list
> Subject: [R] x %>% y as an alternative to which( x > y)
>
> 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.
>
> ______________________________________________
> 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.
More information about the R-help
mailing list