Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R: sort a data frame by a numeric column of repeated numbers

I have a data frame that looks like this:

 Gene         Cluster       
  A              0        
  B              0        
  C              7        
  D              7        
  F              1        
  G              1
  H              2
  I              3
  L              8
  M              8
  Z              8    

I would like to sort the data.frame by the numeric column "cluster" in the following order:

order = c(1,0,8,7,3,2)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

With a column of factors I usually use "match" but with a numeric column of repeated numbers I don’t know exactly how to sort the rows with my desired order.

Can anyone help me please?

>Solution :

Your thought to use match is correct, now just include order(.) as well:

dat[order(match(dat$Cluster, c(1,0,8,7,3,2))),]
#    Gene Cluster
# 5     F       1
# 6     G       1
# 1     A       0
# 2     B       0
# 9     L       8
# 10    M       8
# 11    Z       8
# 3     C       7
# 4     D       7
# 8     I       3
# 7     H       2
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading