0

I had a problem to deal with the reordering of rows in a dataset.

 >df
 ID  TAG   TIME  READ1  READ2
 1   0     0     NA     NA
 1   1     0     0.0    NA
 1   0     0     NA     1.0
 1   0     1     0.5    NA   
 1   1     2     NA     NA
 1   0     2.1   1.5    2.5   
 2   0     0     NA     NA
 2   1     0     0.0    NA
 2   0     0     NA     1.5
 2   0     1     0.5    NA   
 2   1     1.5   NA     NA
 2   0     2.5   2.0    2.9

My goal is to order the dataset by each ID and put the row with the first TAG=1 on the first. All other rows will be ordered by TIME. The result should be:

 >df1
 ID  TAG   TIME  READ1  READ2
 1   1     0     0.0    NA
 1   0     0     NA     NA
 1   0     0     NA     1.0
 1   0     1     0.5    NA   
 1   1     2     NA     NA
 1   0     2.1   1.5    2.5   
 2   1     0     0.0    NA
 2   0     0     NA     NA
 2   0     0     NA     1.5
 2   0     1     0.5    NA   
 2   1     1.5   NA     NA
 2   0     2.5   2.0    2.9
nobody
  • 19,010
  • 17
  • 53
  • 73

1 Answers1

0

You're ordering ascending by ID, then ascending by TIME, then descending by TAG. This can be done with:

df[order(df$ID, df$TIME, -df$TAG),]
#    ID TAG TIME READ1 READ2
# 2   1   1  0.0   0.0    NA
# 1   1   0  0.0    NA    NA
# 3   1   0  0.0    NA   1.0
# 4   1   0  1.0   0.5    NA
# 5   1   1  2.0    NA    NA
# 6   1   0  2.1   1.5   2.5
# 8   2   1  0.0   0.0    NA
# 7   2   0  0.0    NA    NA
# 9   2   0  0.0    NA   1.5
# 10  2   0  1.0   0.5    NA
# 11  2   1  1.5    NA    NA
# 12  2   0  2.5   2.0   2.9

Check out this question for more details on ordering by multiple columns.

Community
  • 1
  • 1
josliber
  • 41,865
  • 12
  • 88
  • 126