R:如何获取时间序列数据中datetime列的最大值 [英] R: How to get the maximum value of a datetime column in a time series data
本文介绍了R:如何获取时间序列数据中datetime列的最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在处理时间序列数据.我有2个日期时间列和1个财政周列.我举了一个例子,说明我的情况如下,我需要获得EditDate的最大值.
I am working on a time series data. I have 2 date time columns and 1 fiscal week column. I have given an example where I have a situation like below and I need to get the MAX of the 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)
这将返回
+---------------------+-------+---------+-------------+--------------------+----+
| 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来运行,但是运行需要很多时间.
I tried using sqldf using the library(RH2) but it takes a lot of time to run.
df2 <- sqldf("SELECT * FROM df1
WHERE (EditDate = (SELECT MAX(EditDate) FROM df1))
ORDER BY EditDate ASC")
我不确定是否可以使用dplyr软件包来完成.有人可以提供有关我如何使用dplyr或任何其他替代方案进行优化的意见吗?
I am not sure if it could be done using the dplyr package. Could someone provide inputs on how I could optimize this using dplyr or any other alternatives?
推荐答案
这里是一个底数为R的班轮
Here's one liner with base 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
这篇关于R:如何获取时间序列数据中datetime列的最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文