2

I have a big file that looks like this:

  Col1_names     Col1_values      Col2_names      Col2_values
      a               0.2              b               0.12
      c               0.12             m               0.2
      d               0.5              n               0.21
      g               0.3              w               0.1
      p               0.1              y               0.3
      h               0.32             z               0.01

I need to sort the data.frame below so that it will look like this:

  Col1_names      Col1_values     Col2_names        Col2_values
       p              0.1              z                 0.01
       c              0.12             w                 0.1                
       a              0.2              b                 0.12                     
       g              0.3              m                 0.2             
       h              0.32             n                 0.21              
       d              0.5              y                 0.3

In other words I need to sort in ascending order each column containing values ("Col1_values"). I'm quite new in R, so it seems too difficult for me.

In the real case, the data.frame contains around 200 lists.

halfer
  • 19,824
  • 17
  • 99
  • 186
Fuv8
  • 885
  • 3
  • 11
  • 21
  • 1
    Can you provide some example data? http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example Also, have you looked at `?order` and `?sort`? – Roman Luštrik Apr 24 '13 at 14:08
  • The problem is that I don't know how to start..I tried sort, but it does not sort in the way I wont.. – Fuv8 Apr 24 '13 at 14:10
  • 1
    Have you tried the examples in the mentioned functions? Especially this line in `order`: `rbind(x, y, z)[, order(x, -y, z)]` – Roman Luštrik Apr 24 '13 at 14:12
  • three questions: 1) is it a named list? 2) do you want to keep the result still as a list or bind them together as a data.frame? 3) if the answer to (2) is bind them together, then, do you want to sort within each data.frame and then bind or bind and then sort? – Arun Apr 24 '13 at 14:25
  • Hi Arun! I would like to bind the results all together as a data.frame, so I think the simplest way (I don't know how to do it) is to consider each "Col_name" and "Col_values" as two lists of a sub data.frame so the main data.frame will result as a list of data.frames to be able to sort each "Col_name" column according to the respective "Col_value" column. For the first question, yes, it is a named list. – Fuv8 Apr 24 '13 at 14:32
  • @Fuv8, You've to read my comment carefully and answer once again. BTW, your result `Col1_values` is *not* sorted by descending order.. is it (ex: 0.1 occurs before 0.5)? – Arun Apr 24 '13 at 14:34
  • Sorry Arun, I made a mistake writing the question, but I edited soon! – Fuv8 Apr 24 '13 at 14:38
  • 1
    1) It's still not sorted in `ascending` order. 2) I can not answer your question unless I get my questions clarified (questions from 2 comments before). Good luck! – Arun Apr 24 '13 at 14:40
  • Hi Arun, I edited again...sorry again but I have too much work so I' m a little bit tired.. – Fuv8 Apr 24 '13 at 15:28

1 Answers1

1

Something along these lines should do this

txt <- "Col1_names     Col1_values      Col2_names      Col2_values
          a               0.20             b               0.12
          c               0.12             m               0.2
          d               0.5              n               0.21
          g               0.30             w               0.10
          p               0.1              y               0.30
          h               0.32             z               0.01"

dat <- read.table(text = txt, header = TRUE)
dat[order(dat$Col1_values), ]

##   Col1_names Col1_values Col2_names Col2_values
## 5          p        0.10          y        0.30
## 2          c        0.12          m        0.20
## 1          a        0.20          b        0.12
## 4          g        0.30          w        0.10
## 6          h        0.32          z        0.01
## 3          d        0.50          n        0.21
dickoa
  • 18,217
  • 3
  • 36
  • 50