user3036416
user3036416

Reputation: 1255

Order multiple rows in a data frame

I have a data frame like this

ID EPOCH
B    2
B    3
A    1
A    2
A    3
C    0

and what I would like to do is to order it by the ID first appearance date (i.e. the minimum value of EPOCH for each ID) so that I get

ID EPOCH
C    0
A    1
A    2
A    3
B    2
B    3

I managed only to order the data frame according to Epoch and than ID

df[order(df$EPOCH,df$ID),]

but than it is no more clustered by ID, i.e.

C  0
A  1
A  2
B  2
A  3
B  3

Many thanks

Upvotes: 2

Views: 98

Answers (1)

Jan van der Laan
Jan van der Laan

Reputation: 8105

First add a column with the minimum EPOCH for each ID to the data.frame:

data <- read.table(textConnection("ID EPOCH
B    2
B    3
A    1
A    2
A    3
C    0"), header=TRUE)

a <- aggregate(data$EPOCH, data["ID"], min)
names(a)[2] <- "min_EPOCH"

data <- merge(data, a)

Then sort on that new column:

o <- order(data$min_EPOCH, data$ID, data$EPOCH)
data[o, ]

Upvotes: 3

Related Questions