user1434997 user1434997 - 2 months ago 8
R Question

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

I have a dataset sampled irregularly at 30 min frequency as follows. I need to extract the index of last timestamp on each day. The dataset is as follows:

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/25/2011 7:30")


I have converted it into POSIXlt object as follows.

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


However, I am not able to extract the last index of each day. I would want an output that as index of last time stamp for each day i.e. my output will be

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


Any help will be appreciated. Thanks!

Answer

Try:

data:

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

Code:

lapply(split(datetime, substr(datetime, 1,9)), function(x) {
    match(x[length(x)], datetime)
})

If you want to also return the value at the index, you can tweak the code like the following:

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

Output1:

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

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

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

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

Output2:

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

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

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

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