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.
Hi Just order your table output. xx<-sample(letters[1:5], 100, replace=T) yy<-table(xx) barplot(yy[order(yy, decreasing=T)]) Regards Petr> > > 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 histogramwith> 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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
On 03/08/2012 03:46 PM, Manish Gupta 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? >Hi Manesh, The freq function (prettyR) returns a vector (or a list of vectors if more than one variable is passed) with the same information as the table function. However, freq orders the table in descending frequency by default. Jim
Here is a quick start. You will have to either greatly reduce the length of the names or play around with various plot or barplot options to get the x labels. Have a look at ?plot.default and ?barplot for more information John Kane Kingston ON Canada ===============================================aa <- c("Acute lymphoblastic leukemia (childhood)", "Adiponectin levels", "Adiponectin levels", "Adiponectin levels", "Adiponectin levels", "Adiponectin levels", "Adiposity", "Adiposity", "Adiposity", "Adiposity", "Age-related macular degeneration", "Age-related macular degeneration", "Aging (time to death)", "Aging (time to event)", "Aging (time to event)", "Aging (time to event)", "Aging (time to event)", "AIDS", "AIDS", "AIDS") (xx <- table(aa)) barplot(xx) ================================================> -----Original Message----- > From: mandecent.gupta at gmail.com > Sent: Wed, 7 Mar 2012 20:46:18 -0800 (PST) > To: r-help at r-project.org > Subject: [R] How to sort frequency distribution table? > > 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.____________________________________________________________ FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!
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