1

I want to order factors depending on the sum of the observations.

df <- mtcars[,c('cyl', 'gear')]
df$cyl <- factor(df$cyl)

str(df)
'data.frame':   32 obs. of  2 variables:
 $ cyl : Factor w/ 3 levels "4","6","8": 2 2 1 2 3 2 3 1 1 2 ...
 $ gear: num  4 4 4 3 3 3 3 4 4 4 ...

Please see the sum cyl in the rows.

addmargins(table(df))
     gear
cyl    3  4  5 Sum
  4    1  8  2  11
  6    2  4  1   7
  8   12  0  2  14
  Sum 15 12  5  32

Ascending the ordering of cyl should be 6, 4 and 8. I mean

 $ cyl : Factor w/ 3 levels "6","4","8"

How can I realize that?

The background of the question is: Order groups in a stacked stripchart by sum in R.

Community
  • 1
  • 1
buhtz
  • 10,774
  • 18
  • 76
  • 149

1 Answers1

1

You can feed the desired ordering into the levels argument of factor. In this case, to do it on the fly, replace your second line with:

df$cyl <- factor(df$cyl, levels=names(sort(table(df$cyl))))

Then you get

str(df)
'data.frame':   32 obs. of  2 variables:
 $ cyl : Factor w/ 3 levels "6","4","8": 1 1 2 1 3 1 3 2 2 1 ...
 $ gear: num  4 4 4 3 3 3 3 4 4 4 ...

Here, the frequency values are calculated using table and then sorted in ascending order using sort. The result is fed to the levels argument which determines the ordering of the levels in factor.

Note: I originally wrapped names in as.numeric, but then found this is not necessary. The factor function performs the necessary conversion.

lmo
  • 37,904
  • 9
  • 56
  • 69