从 30 分钟数据中提取 R 中一天的最后一个时间戳

Extract last time stamp of a day in R from 30 minute data

我有一个以 30 分钟频率不规则采样的数据集,如下所示。我需要提取每天最后一个时间戳的索引。数据集如下:

datetime <-c("8/19/2011 16:00",
"8/19/2011 17:30",
"8/19/2011 18:30",
"8/19/2011 19:30",
"8/22/2011 4:00",
"8/22/2011 6:00",
"8/22/2011 7:00",
"8/22/2011 19:00",
"8/22/2011 19:30",
"8/23/2011 4:00",
"8/24/2011 5:30",
"8/24/2011 7:00",
"10/25/2011 7:30")

我已经将它转换成 POSIXlt 对象如下。

datetime <- strptime(datetime, format="%m/%d/%Y  %H:%M")
datetime <- as.POSIXlt(datetime)

但是,我无法提取每天的最后一个索引。我想要一个输出作为每天最后一个时间戳的索引,即我的输出将是

list of (4, 9, 10,12,13) corresponding to datetime values of 
"8/19/2011 19:30"
"8/22/2011 19:30"
"8/23/2011 4:00"
"8/24/2011 7:00"
"10/25/2011 7:30"

如有任何帮助,我们将不胜感激。谢谢!

将这些字符值转换为日期时间,然后按日期(日-月)split。在每个日期内,使用 which.max:

选择最后一个值
dt <- as.POSIXct(datetime, format="%m/%d/%Y %H:%M") 
lapply( split( dt, format(dt,"%m-%d") ), function(d) as.POSIXct(d[which.max(d)] ) )
$`08-19`
[1] "2011-08-19 19:30:00 PDT"

$`08-22`
[1] "2011-08-22 19:30:00 PDT"

$`08-23`
[1] "2011-08-23 07:30:00 PDT"

$`08-24`
[1] "2011-08-24 07:00:00 PDT"

尝试:

数据:

 datetime <- c("8/19/2011 16:00", "8/19/2011 17:30", "8/19/2011 18:30", "8/19/2011 19:30", "8/22/2011 4:00", "8/22/2011 6:00", "8/22/2011 7:00", "8/22/2011 19:00", "8/22/2011 19:30", "8/23/2011 4:00", "8/24/2011 5:30", "8/24/2011 7:00", "8/23/2011 7:30", "12/23/2012 19:23", "11/24/2015 7:13")

代码:

splitter <- strftime(strptime(datetime,"%m/%d/%Y %H:%M"), "%m/%d/%Y")    
lapply(split(datetime, splitter), function(x) {
        match(x[length(x)], datetime)
    })

如果您还想 return 索引处的值,您可以像下面这样调整代码:

lapply(split(datetime, splitter), function(x) {
    val_index <- match(x[length(x)], datetime)
    c(val_index,datetime[val_index])
})

输出1:

$`08/19/2011`
[1] 4

$`08/22/2011`
[1] 9

$`08/23/2011`
[1] 13

$`08/24/2011`
[1] 12

$`11/24/2015`
[1] 15

$`12/23/2012`
[1] 14

输出2:

$`08/19/2011`
[1] "4"               "8/19/2011 19:30"

$`08/22/2011`
[1] "9"               "8/22/2011 19:30"

$`08/23/2011`
[1] "13"             "8/23/2011 7:30"

$`08/24/2011`
[1] "12"             "8/24/2011 7:00"

$`11/24/2015`
[1] "15"              "11/24/2015 7:13"

$`12/23/2012`
[1] "14"               "12/23/2012 19:23"