[R] How to sort frequency distribution table?

Greg Snow 538280 at gmail.com
Fri Mar 9 18:00:12 CET 2012


R tends to see the ordering of factor levels as a property of the data
rather than a property of the table/graph.  So it is generally best to
modify the data object (factor) to represent what you want rather than
look for an option in the table/plot function (this will also be more
efficient in the long run).  Here is a simple example using the
reorder function:

> tmp <- factor(sample( letters[1:5], 100, TRUE ))
> table(tmp)
tmp
 a  b  c  d  e
20 20 19 18 23
> tmp2 <- reorder(tmp, rep(1,length(tmp)), sum)
> table(tmp2)
tmp2
 d  c  a  b  e
18 19 20 20 23
> tmp2 <- reorder(tmp, rep(-1,length(tmp)), sum)
> table(tmp2)
tmp2
 e  a  b  c  d
23 20 20 19 18


On Wed, Mar 7, 2012 at 9:46 PM, Manish Gupta <mandecent.gupta at gmail.com> wrote:
> Hi,
>
> I am working on categorical data with column as disease name(categaory).
>
> My input data is
>  [1] Acute lymphoblastic leukemia (childhood)
>  [2] Adiponectin levels
>  [3] Adiponectin levels
>  [4] Adiponectin levels
>  [5] Adiponectin levels
>  [6] Adiponectin levels
>  [7] Adiposity
>  [8] Adiposity
>  [9] Adiposity
> [10] Adiposity
> [11] Age-related macular degeneration
> [12] Age-related macular degeneration
> [13] Aging (time to death)
> [14] Aging (time to event)
> [15] Aging (time to event)
> [16] Aging (time to event)
> [17] Aging (time to event)
> [18] AIDS
> [19] AIDS
> [20] AIDS
> .....
>
>
> when i use table command, i get
>
>
> [,1]
> Acute lymphoblastic leukemia (childhood)                             1
> Adiponectin levels
> 5
> Adiposity
> 4
> Age-related macular degeneration
> 2
> Aging (time to death)
> 1
> ......
>
> But i need to sort this table by frequency and need to plot a histogram with
> lable first column (e.g. Adiposity , Age-related macular degeneration  as
> bar name). How can i do it?
>
> Regards
>
> --
> View this message in context: http://r.789695.n4.nabble.com/How-to-sort-frequency-distribution-table-tp4455595p4455595.html
> Sent from the R help mailing list archive at Nabble.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.



-- 
Gregory (Greg) L. Snow Ph.D.
538280 at gmail.com



More information about the R-help mailing list