user2575429
user2575429

Reputation: 111

Sort data frame column based on another list

I have a data frame which looks like this

 aa   bb   cc   
 -------------
 1    aa    1   
 1    dd    10   
 2    bb    11   
 2    cc    20   
 3    cc    29   
 3    dd    39   
 3    aa    33   
 4    ee    35   
 4    cc    14   

I want above data frame to be ordered by column "bb" a different an order I have as an extra input. for example column "bb" should be ordered like this {cc, aa, dd, bb, ff, ee} but row integrity should be preserved.

 aa   bb   cc   
 -------------
 1    aa    1   
 1    dd    10   
 2    cc    20   
 2    bb    11  
 3    cc    20   
 3    aa    33   
 3    dd    39   
 4    cc    14   
 4    ee    35   

R input code

d1 <- read.table(header = TRUE, sep = ";", text = 
"aa;bb;cc
1;aa;1
1;dd;10
2;cc;20
2;bb;11
3;cc;20
3;aa;33
3;dd;39
4;cc;14
4;ee;35"   )

d2 <- c( "cc","aa","dd","bb","ff","ee")

Upvotes: 1

Views: 3566

Answers (2)

Heroka
Heroka

Reputation: 13169

You can use the nice properties of factors (numerical variables with labels) here:

#create a factor, level it by d2
d1$bb <- factor(d1$bb, levels=d2)

#order
output <- d1[order(d1$bb),] ## i made a simple edit here
    > output
  aa bb cc
3  2 cc 20
5  3 cc 20
8  4 cc 14
1  1 aa  1
6  3 aa 33
2  1 dd 10
7  3 dd 39
4  2 bb 11
9  4 ee 35

Upvotes: 4

Ven Yao
Ven Yao

Reputation: 3710

d1 <- read.table(header = TRUE, sep = ";", text = 
                   "aa;bb;cc
1;aa;1
1;dd;10
2;cc;20
2;bb;11
3;cc;20
3;aa;33
3;dd;39
4;cc;14
4;ee;35")

library(plyr)
d2 <- ddply(d1, .(aa), function(df){
  df$bb <- ordered(df$bb, levels=c( "cc","aa","dd","bb","ff","ee"))
  return(df)
})

d2
#   aa bb cc
# 1  1 aa  1
# 2  1 dd 10
# 3  2 cc 20
# 4  2 bb 11
# 5  3 cc 20
# 6  3 aa 33
# 7  3 dd 39
# 8  4 cc 14
# 9  4 ee 35

Upvotes: 1

Related Questions