[R] Reordering the results from table(cut()) by break argument

David Winsemius dwinsemius at comcast.net
Wed Dec 2 19:17:40 CET 2009


On Dec 2, 2009, at 12:18 PM, Mark Heckmann wrote:

> I have a vector and need to count how many data points fall inside  
> each bin:
>
> dat <- rnorm(100)
> breaks <- -3:3
> table((cut(dat, breaks)))
>
> (-3,-2] (-2,-1]  (-1,0]   (0,1]   (1,2]   (2,3]
>      3      13      42      30      12       0
>
> if I reverse the breaks vector, the results remains the same:
> breaks <- rev(breaks)
> table((cut(dat, breaks)))
>
> (-3,-2] (-2,-1]  (-1,0]   (0,1]   (1,2]   (2,3]
>      3      13      42      30      12       0
>
> What I would like is break to also determine the order of the table  
> output, in this case it should also be reversed, like:

Instead of reversing the breaks (which doesn't work because factors  
obey a very strange physics) reverse the table:

 > rev(table((cut(dat, breaks))))

   (2,3]   (1,2]   (0,1]  (-1,0] (-2,-1] (-3,-2]
       2      21      25      35      14       3


> ( 3, 2] ( 2, 1]  ( 1,0]   (0,-1]   (-1,-2]   (-2,-3]
>      0      12      30      42      13       3
>
> Thus I would like to reorder the vector using break, but I do not  
> know how.
>
> TIA
> Mark
> –––––––––––––––––––––––––––––––––––––––
> Mark Heckmann
> Dipl. Wirt.-Ing. cand. Psych.
> Vorstraße 93 B01
> 28359 Bremen
> Blog: www.markheckmann.de
> R-Blog: http://ryouready.wordpress.com
>
> ______________________________________________
> 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.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list