[R] sorting factor levels by data frequency of levels
Jim Lemon
jim at bitwrit.com.au
Sat Nov 17 10:41:12 CET 2007
Shoaaib Mehmood wrote:
> using an example from r online help
>
>
>>state <- c("tas", "sa", "qld", "nsw", "nsw", "nt", "wa", "wa",
>
> "qld", "vic", "nsw", "vic", "qld", "qld", "sa", "tas",
> "sa", "nt", "wa", "vic", "qld", "nsw", "nsw", "wa",
> "sa", "act", "nsw", "vic", "vic", "act")
>
>>statefac<-factor(state)
>
>
> now if i use levels function to print factor levels they are shown
> sorted by alphabetical order. the output is shown below
>
>
>>levels(statefac)
>
> [1] "act" "nsw" "nt" "qld" "sa" "tas" "vic" "wa"
>
> I WANT THE FACTOR LEVELS TO BE SORTED IN ORDER OF DESCENDING DATA
> FREQUENCY IN EACH LEVEL. IN OTHER WORDS I WANT THE LEVEL WHICH HAS THE
> HIGHEST FREQUENCY IN STATE TO APPEAR FIRST, FOLLOWED BY ONE WHICH HAS
> THE SECOND HIGHEST FREQUENCY AND SO ON.
>
levels(state)<-levels(state)[rev(order(tabulate(state)))]
Jim
More information about the R-help
mailing list