[R] Using and abusing %>% (was Re: Why can't I access this type?)

Patrick Connolly p_connolly at slingshot.co.nz
Thu Mar 26 07:48:52 CET 2015


On Wed, 25-Mar-2015 at 03:14PM +0100, Henric Winell wrote:

...

|> Well...  Opinions may perhaps differ, but apart from '%>%' being
|> butt-ugly it's also fairly slow:

Beauty, it is said, is in the eye of the beholder.  I'm impressed by
the way using %>% reduces or eliminates complicated nested brackets.
In this tiny example it's not obvious but it's very clear if the
objective is to sort the dataframe by three or four columns and
various lots of aggregation then returning a largish number of
consecutive columns, omitting the rest.  It's very easy to see what's
going on without the need for intermediate objects.

|>
|>  .....

|> Unit: microseconds
|> 
|>    expr
|>  subset(all.states, all.states$Frost > 150, select = c("state",
|> "Frost"))
|>                        all.states[all.states$Frost > 150,
|> c("state", "Frost")]
|>                    all.states %>% filter(Frost > 150) %>%
|> select(state, Frost)
|>       min       lq      mean    median        uq      max neval cld
|>   139.112  148.673  163.3960  159.1760  170.7895 1763.200  1000  b
|>   104.039  111.973  127.2138  120.4395  128.6640 1381.809  1000 a
|>  1010.076 1033.519 1133.1469 1107.8480 1175.1800 2932.206  1000   c

It's no surprise that instructing a computer in something closer to
human language is an order of magnitude slower.  I'm sure you'd get
something even quicker using machine code.  I spend 3 or 4 orders of
magnitude more time writing code than running it.  It's much more
important to me to be able to read and modify than it is to have it
run at optimum speed.

|> 
|> Of course, this doesn't matter for interactive one-off use.  But
|> lately I've seen examples of the '%>%' operator creeping into
|> functions in packages. 

That could indicate that %>% is seductively easy to use.  It's
probably true that there are places where it should be done the hard
way.


|>  However, it would be nice to see a fast pipe operator as part of
|> base R.

|> 
|> 
|> Henric Winell
|> 

-- 
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.   
   ___    Patrick Connolly   
 {~._.~}                   Great minds discuss ideas    
 _( Y )_  	         Average minds discuss events 
(:_~*~_:)                  Small minds discuss people  
 (_)-(_)  	                      ..... Eleanor Roosevelt
	  
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.



More information about the R-help mailing list