Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

r - how to reorder a factor in a dataframe with fct_reorder?

Consider the following example

> library(forcats)
> library(dplyr)
> 
> 
> dataframe <- data_frame(var = c(1,1,1,2,3,4),
+                         var2 = c(10,9,8,7,6,5))
> dataframe
# A tibble: 6 x 2
    var  var2
  <dbl> <dbl>
1  1.00 10.0 
2  1.00  9.00
3  1.00  8.00
4  2.00  7.00
5  3.00  6.00
6  4.00  5.00

I create a factor variable

> dataframe <- dataframe %>% mutate(myfactor = factor(var))
> 
> dataframe$myfactor
[1] 1 1 1 2 3 4
Levels: 1 2 3 4

I do not understand what is the correct syntax (and the logic) to reorder this factor according to some other computation done at the factor level.

For instance, I would like to reorder my factors according to decreasing values of

> data_rank <- dataframe %>% group_by(myfactor) %>% summarise(rank_var = mean(var2))

> data_rank
# A tibble: 4 x 2
  myfactor rank_var
  <fct>       <dbl>
1 1            9.00
2 2            7.00
3 3            6.00
4 4            5.00

So 4 would be first, 3 would be second, etc.

What is the syntax to do so with fct_reorder, and what is the logic behind it?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Suppose your dataframe is:

dataframe <- data_frame(var = c(1,1,1,2,3,4),var2 = c(10,2,0,15,6,5))
dataframe <- dataframe %>% mutate(myfactor = factor(var))
dataframe$myfactor

[1] 1 1 1 2 3 4
Levels: 1 2 3 4

Now if you want to reorder your factor, where the order is given by the output of a certain function fun on a certain vector x then you can use fct_reorder in the following way:

dataframe$myfactor= fct_reorder(f = dataframe$myfactor,x = dataframe$var2,fun = mean)
dataframe$myfactor
[1] 1 1 1 2 3 4
Levels: 1 4 3 2

mean of dataframe$var2 for each factor will be calculated and sorted in ascending order by default to order the factor.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...