-2

I have a question. I want to sort the TIME in ascending order for each ID, I tried simple ways but it stacks same time points of all IDs. I want separate IDs with time sorted on the basis of ascending order. thanks in advance.

The data is

  ID    TIME    DV
1   0   0
1   2   1024.88
1   12  1229.82
1   36  1269.47
1   4   1274.37
1   24  1274.37
1   8   1274.37
1   48  1274.37
2   0   0
2   48  924.84
2   4   1356.87
2   36  1459.79
2   12  1467.43
2   24  1467.43
2   8   1467.43
2   2   1467.43
jogo
  • 12,469
  • 11
  • 37
  • 42

2 Answers2

1

with data.tableyou could do something like this:

dt <- data.table(ID = c(1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2),
                 TIME = c(0,2,12,36,4,24,8,48,0,48,4,36,12,24,8,2),
                 EV = c(0,1024.88,1229.82,1269.47,1274.37,1274.37,1274.37,1274.37,
                        0,924.84,1356.87,1459.79,1467.43,1467.43,1467.43,1467.43))

dt[order(ID, TIME)]

    ID TIME      EV
 1:  1    0    0.00
 2:  1    2 1024.88
 3:  1    4 1274.37
 4:  1    8 1274.37
 5:  1   12 1229.82
 6:  1   24 1274.37
 7:  1   36 1269.47
 8:  1   48 1274.37
 9:  2    0    0.00
10:  2    2 1467.43
11:  2    4 1356.87
12:  2    8 1467.43
13:  2   12 1467.43
14:  2   24 1467.43
15:  2   36 1459.79
16:  2   48  924.84
Jakob Gepp
  • 463
  • 3
  • 10
0

Your question is not reproducible (please see How to make a great R reproducible example?). Here is an easy solution using the dplyr package, however.

df <- data.frame(ID = c(1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2),
             TIME = c(0,2,12,36,4,24,8,48,0,48,4,36,12,24,8,2),
             EV = c(0,1024.88,1229.82,1269.47,1274.37,1274.37,1274.37,1274.37,
             0,924.84,1356.87,1459.79,1467.43,1467.43,1467.43,1467.43))

library(dplyr)

df %>% 
  arrange(ID, TIME)

ID TIME    EV
1    0    0.00
1    2 1024.88
1    4 1274.37
1    8 1274.37
1   12 1229.82
1   24 1274.37
1   36 1269.47
1   48 1274.37
2    0    0.00
2    2 1467.43
2    4 1356.87
2    8 1467.43
2   12 1467.43
2   24 1467.43
2   36 1459.79
2   48  924.84  

Edit: Is this not what you want?

tifu
  • 1,352
  • 6
  • 17
  • Hmm i am very sorry but i am from medical field dosnt know much about these processes. Still it is not working for mee – Muhammad Faisal Dec 21 '17 at 13:56
  • I edited my answer to make it fully reproducible. Working for you now? You obviously need to have `dplyr` installed on your machine. – tifu Dec 21 '17 at 14:31