[R] date
Val
v@|kremk @end|ng |rom gm@||@com
Mon Sep 21 20:37:12 CEST 2020
Hi All,
I am trying to sort dates within a group. My sample data is
df <-read.table(text="ID date
A1 09/17/04
A1 01/27/05
A1 05/07/03
A2 05/21/17
A2 09/12/16
A3 01/25/13
A4 09/27/19",header=TRUE,stringsAsFactors=F)
df$date2 = as.Date(strptime(df$date,format="%m/%d/%y"))
df$date =NULL
I want to sort date2 from recent to oldest. within the ID group and
I used this,
df <- df[order(df$ID, rev((df$date2))),]. It did not work and teh
output is shown below.
ID date2
2 A1 2005-01-27
3 A1 2003-05-07
1 A1 2004-09-17
5 A2 2016-09-12
4 A2 2017-05-21
6 A3 2013-01-25
7 A4 2019-09-27
What am I missing?
Thank you.
More information about the R-help
mailing list