RでFactorの頻度(度数)順にFactorの順を並べ替える。

Sorting factor levels by data frequency of levels


table function (arg:factors) returns table class (which seems to be based on numeric vector. Check mode(table(factor(c(1,2,3)))))


order function (arg:vector) returns index vector (whose number element means the order in the vector.)


e.g.)

When outcome.state has many states names.(factor)


names( table(outcome.state)[ order(table( outcome.state) , decreasing = TRUE) ][1:20])


returns top 20 states listed.


# Maybe by using "with" function, this code can be shortened.