2

I am Trying to order a DF by date.time (POSIXct object) with the most recent date.time first

i.e. in reverse

df1<-structure(list(date.time = structure(c(1368066412, 1365895151, 
1365969188, 1366105495, 1367433149, 1367604510, 1367614808, 1367619743, 
1368005216, 1368011698, 1366244224, 1366414926, 1367513240, 1367624274, 
1367640074), class = c("POSIXct", "POSIXt"), tzone = ""), station = c("M1", 
"F1", "F3", "F4", "F5", "L1", "L2", "L4", "L5", "L7", "F1", "F3", 
"F4", "L2", "L4"), code = c(10184, 10888, 10888, 10888, 10888, 
10888, 10888, 10888, 10888, 10888, 10889, 10889, 10889, 10889, 
10889)), .Names = c("date.time", "station", "code"), row.names = c(NA, 
15L), class = "data.frame")

initial ordering:

df1[with(df1, order(code, date.time)), ]

This works fine however what i want is for the ordering of date.time section to be in reverse, with the most recent date.time stamp being ordered first.

Failed attempts:

df1[with(df1, order(code, -date.time)), ]

negative value does not work with a POSIXct object

df1[with(df1, order(code, rev(date.time))), ]

orders the DF in a weird way and not as desired

Any help would be great!

4

1 回答 1

3

也许是这样的:

df2 <- df1[order(df1$code, -as.numeric(df1$date.time)), ]
df2    

unclass,as.vector还将 POSIXct 对象转换为数字。as.integer也是一种选择。

于 2013-09-08T21:32:19.700 回答