-2

I have a table listing the proportion that each class takes up in a categorical variable. It is ordered alphabetically and I'd like to order it by size.

I tried to make it a data frame and then arrange that data frame by the proportions. But, it shows there is only one column.

When I called names(), the result is

[1] "format(prop.table(table(Category)), scientific = F)"

cat_prop <- as.data.frame(format(prop.table(table(Category)), scientific = F))
arrange(cat.prop)

This gets me the list of classes and their proportion. How do I arrange that by value?

seb
  • 1
  • 1
  • Please read https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example on how to produce reproducible examples. We need sample data and output to answer your question. – hmhensen Jan 05 '19 at 01:42

1 Answers1

2

Seems like sort should do it. E.g.,

> vec <- c('a', 'a', 'b', 'c', 'c', 'c')
> prop.table(table(vec))
vec
        a         b         c 
0.3333333 0.1666667 0.5000000 
> sort(prop.table(table(vec)), decreasing = TRUE)
vec
        c         a         b 
0.5000000 0.3333333 0.1666667

Just FYI, coercing a table to a dataframe requires as.data.frame.table(); as.dataframe() might not behave the way you want

Joseph Clark McIntyre
  • 1,094
  • 1
  • 6
  • 6