2

Say I have a dataframe like this:

Date      A   B   C    D   E   H
1/28/2013  56  51  35  44  08  18  
1/25/2013  38  56  28  39  23  32  
1/21/2013  36  51  45  25  40  08

what I want to do is sort each row's ABCDE column by their values. So I can get is:

Date       A   B   C    D   E   H    
1/28/2013  08  35  44  51  56  18  
1/25/2013  23  28  38  39  56  32  
1/21/2013  25  36  40  45  51  08
Thomas
  • 43,637
  • 12
  • 109
  • 140
ToToRo
  • 373
  • 1
  • 5
  • 12
  • Please read about how to post your data in [a reproducible format](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), e.g. using `dput`. – Thomas Feb 23 '14 at 17:27

1 Answers1

2

You can use apply:

dat[c("A", "B", "C", "D", "E")] <- t(apply(dat[c("A", "B", "C", "D", "E")], 
                                           1, sort))

#        Date  A  B  C  D  E  H
# 1 1/28/2013  8 35 44 51 56 18
# 2 1/25/2013 23 28 38 39 56 32
# 3 1/21/2013 25 36 40 45 51  8

where dat is the name of your data frame.

Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168