本文介绍了如何获取每个ID /日期的最新数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含id,POSIXct(Date& Time)的数据框

I have a data frame that contains id, POSIXct(Date & Time)

> myData

   Tpt_ID    Tpt_DateTime               Value
1  1         2013-01-01 15:17:21 CST    10
2  2         2013-01-01 15:18:32 CST    5
3  3         2013-01-01 16:00:02 CST    1
4  1         2013-01-02 15:10:11 CST    15
5  2         2013-02-02 11:18:32 CST    6
6  3         2013-02-03 12:00:02 CST    2
7  1         2013-01-01 19:17:21 CST    21
8  2         2013-02-02 20:18:32 CST    8
9  3         2013-02-03 22:00:02 CST    3

我想获取每个日期和ID的最后一个值

I'd like to get last Value for each Date and ID

例如,

Tpt_ID   Tpt_DateTime               Value
2        2013-01-01 15:18:32 CST    5
3        2013-01-01 16:00:02 CST    1
1        2013-01-02 15:10:11 CST    15
1        2013-01-01 19:17:21 CST    21
2        2013-02-02 20:18:32 CST    8
3        2013-02-03 22:00:02 CST    3

数据样本:

structure(list(Tpt_ID = c(1, 2, 3, 1, 2, 3, 1, 2, 3), Tpt_DateTime = structure(c(1357024641, 1357024712, 1357027202, 1357110611, 1359775112, 1359864002, 1357039041, 1359807512, 1359900002), class = c("POSIXct", "POSIXt"), tzone = ""), Value = c(10, 5, 1, 15, 6, 2, 21, 8, 3)), .Names = c("Tpt_ID", "Tpt_DateTime", "Value"), row.names = c(NA, 9L), class = "data.frame")


推荐答案

您可以使用 data.table 语法...

You can do this pretty easily using data.table syntax...

#  Load package
require( data.table )

#  Turn 'data.frame' into 'data.table'
dt <- data.table( df )

#  Make dates from date/time
dt[ , Date:= as.Date( Tpt_DateTime ) ]

#  Get last row of each group
dt[ , .SD[.N] ,  by = c("Tpt_ID" , "Date") ]
#   Tpt_ID       Date        Tpt_DateTime Value
#1:      1 2013-01-01 2013-01-01 11:17:21    21
#2:      2 2013-01-01 2013-01-01 07:18:32     5
#3:      3 2013-01-01 2013-01-01 08:00:02     1
#4:      1 2013-01-02 2013-01-02 07:10:11    15
#5:      2 2013-02-02 2013-02-02 12:18:32     8
#6:      3 2013-02-03 2013-02-03 14:00:02     3




  • 首先我们把你的数据时间日期:
    as.Date(Tpt_DateTime)

    然后,我们使用 .SD 为每个组获取X的数据子集 .N 包含每个组的行数,因此 .SD [.N] 给我们最后一行每组。

    Then we use .SD to get a subset of X's data for each group. .N contains the number of row for each group, so .SD[.N] gives us the last row for each group.

    最后, by = c(Tpt_ID,Date)定义组。

    这篇关于如何获取每个ID /日期的最新数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 20:15