Mark Heckmann
2009-Dec-02 17:18 UTC
[R] Reordering the results from table(cut()) by break argument
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
jim holtman
2009-Dec-02 18:13 UTC
[R] Reordering the results from table(cut()) by break argument
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?
David Winsemius
2009-Dec-02 18:17 UTC
[R] Reordering the results from table(cut()) by break argument
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