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