R:如何获取时间序列数据中日期时间列的最大值

R: How to get the maximum value of a datetime column in a time series data

我正在处理时间序列数据。我有 2 个日期时间列和 1 个财政周列。我给出了一个例子,我遇到了如下情况,我需要获得 EditDate 的最大值。

EditDate <- c("2015-04-01 11:40:13", "2015-04-03 02:54:45","2015-04-07 11:40:13")
ID <- c("DL1X8", "DL1X8","DL1X8")
Avg <- c(38.1517, 38.1517, 38.1517)
Sig <- c(11.45880000, 11.45880000, 11.45880000)
InsertDate <- c("2015-04-03 9:40:00", "2015-04-03 9:40:00",2015-04-10 9:40:00)
FW <- c("39","39","40")

df1 <- data.frame(EditDate , ID, Avg, Sig, InsertDate, FW)

这个returns

+---------------------+-------+---------+-------------+--------------------+----+
|   EditDate          | ID    | Avg     |   Sig       |    InsertDate      | FW |
+---------------------+-------+---------+-------------+--------------------+----+
| 2015-04-01 11:40:13 | DL1X8 | 38.1517 | 11.45880000 | 2015-04-03 9:40:00 | 39 |
| 2015-04-03 02:54:45 | DL1X8 | 38.1517 | 11.45880000 | 2015-04-03 9:40:00 | 39 |
| 2015-04-07 11:40:13 | DL1X8 | 38.1517 | 11.45880000 | 2015-04-10 9:40:00 | 40 |
+---------------------+-------+---------+-------------+--------------------+----+

我想要的期望输出是

+---------------------+-------+---------+-------------+--------------------+----+
|   EditDate          | ID    | Avg     |   Sig       |    InsertDate      | FW |
+---------------------+-------+---------+-------------+--------------------+----+
| 2015-04-07 11:40:13 | DL1X8 | 38.1517 | 11.45880000 | 2015-04-10 9:40:00 | 40 |
+---------------------+-------+---------+-------------+--------------------+----+

我尝试使用库 (RH2) 使用 sqldf,但 运行 需要很多时间。

df2 <- sqldf("SELECT * FROM df1 
                        WHERE (EditDate = (SELECT MAX(EditDate) FROM df1))
                        ORDER BY EditDate ASC")

我不确定是否可以使用 dplyr 包来完成。有人可以提供有关如何使用 dplyr 或任何其他替代方案优化它的意见吗?

使用库 data.tablelubridate 如下:

 library(data.table)
 library(lubridate)
 setDT(df1)
 df1[,EditDate := ymd_hms(EditDate)]
 res <- df1[EditDate = max(EditDate)]

只要lubridate

library(lubridate)

df1[ymd_hms(EditDate)==max(ymd_hms(EditDate)), ]

df1[EditDate==as.character(max(ymd_hms(EditDate))), ]

这是一个底衬 R

df1[which.max(as.POSIXct(df1$InsertDate)), ]
#              EditDate    ID     Avg     Sig         InsertDate FW
# 3 2015-04-07 11:40:13 DL1X8 38.1517 11.4588 2015-04-10 9:40:00 40

data.table

library(data.table)
setDT(df1)[which.max(as.POSIXct(InsertDate))]
#               EditDate    ID     Avg     Sig         InsertDate FW
# 1: 2015-04-07 11:40:13 DL1X8 38.1517 11.4588 2015-04-10 9:40:00 40