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

Hadley Wickham hadley at rice.edu
Tue Sep 13 21:40:19 CEST 2011


> 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"

Use subset:

subset(mydataframeName, myvariableName > 2 & myotherVariableName == "male")

(subset automatically treats NAs as false)

Hadley

-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/



More information about the R-help mailing list