Hercules Apergis
Hercules Apergis

Reputation: 421

How to order data based on repeated dates in R

Suppose that the following dataset is available:

       date           V1     V2
[1,] 1996-01-01   1.4628995  12
[2,] 1996-01-01   0.1972603  11
..............
[3,] 1996-02-01   0.1205479  11
[4,] 1996-02-01   0.9643836   9
..............
[5,] 1996-03-01   0.1972603  14
[6,] 1996-03-01   0.1205479   8

How can i order V1 in ascending order, for example, for each specific date, with the remaining variables, V2,V3... and so on, to follow the ordering. Like this:

           date           V1     V2
    [1,] 1996-01-01   0.1972603  11
    [2,] 1996-01-01   1.4628995  12
    ..............
    [3,] 1996-02-01   0.1205479  11
    [4,] 1996-02-01   0.9643836   9
    ..............
    [5,] 1996-03-01   0.1205479   8
    [6,] 1996-03-01   0.1972603  14

Thank you.

Upvotes: 0

Views: 208

Answers (1)

Andrew Gustar
Andrew Gustar

Reputation: 18425

To sort by date and then by V1...

data <- data[order(as.Date(data$date),data$V1),]

In response to follow-up question in comment below, the rows with the two smallest values of V1 for each date can easily be selected using dplyr...

library(dplyr)
data2 <- data %>% group_by(date) %>% filter(rank(V1,ties.method = "min")<3)

Or, rather less intuitively, using base-R...

data2 <- data[as.logical(ave(data$V1,data$date,FUN=function(v) rank(v,ties.method = "min")<3)),]

You might need to fiddle with the parameters of rank to adjust the treatment of NA and the way it handles ties. See ?rank

Upvotes: 2

Related Questions