RAAAAM
RAAAAM

Reputation: 3380

Order the Data frame based on multiple date column

I have a two column which contains the start date and end date, I am trying to sort the data frame based on the duration among the dates.

Here is some date data to be ordered:

StartDate       EndDate     columnAA

12-Feb-15   30-Dec-17
17-Jun-13   5-JUN-16
20-Jul-13   15-SEP-13
10-Feb-13   14-DEC-15
22-Mar-16   31-MAR-17

Thanks for any suggestions.

Upvotes: 0

Views: 179

Answers (1)

programandoconro
programandoconro

Reputation: 2719

For example, you can have this data frame with 2 dates

survey <- data.frame(date=c("2012/07/26","2012/07/25"),tx_start=c("2012/01/01","2012/01/01"))

Calculate difference between them and create a new column

survey$date_diff <- as.Date(as.character(survey$date), format="%Y/%m/%d")-
  as.Date(as.character(survey$tx_start), format="%Y/%m/%d")

order the data depending of the difference

newdata <- survey[order(survey$date_diff),] 

if you want in descending order use

newdata <- survey[order(-survey$date_diff),] 

Upvotes: 1

Related Questions