反向日期时间排序[R]

时间:2013-09-08 20:48:22

标签: r datetime

我正在尝试通过date.time(POSIXct对象)订购DF,其中包含最新的date.time

即。反过来

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")

初始订购:

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

这样可以正常工作,但我想要的是date.time部分的顺序是反向的,最先订购最新的date.time标记。

尝试失败:

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

负值不适用于POSIXct对象

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

以奇怪的方式命令DF而不是按照需要

任何帮助都会很棒!

1 个答案:

答案 0 :(得分:3)

也许是这样的:

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

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