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

jim holtman jholtman at gmail.com
Wed Dec 2 19:13:48 CET 2009


try this:

> dat <- rnorm(100)
> breaks <- -3:3
> table((cut(dat, breaks)))

(-3,-2] (-2,-1]  (-1,0]   (0,1]   (1,2]   (2,3]
      1      10      35      39      13       2
> x <- table((cut(dat, breaks)))
> rev(x)

  (2,3]   (1,2]   (0,1]  (-1,0] (-2,-1] (-3,-2]
      2      13      39      35      10       1
>


On Wed, Dec 2, 2009 at 12:18 PM, Mark Heckmann <mark.heckmann at gmx.de> 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:
> ( 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list