-1

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",
"10/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 (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"

Any help will be appreciated. Thanks!

user1434997
  • 1,689
  • 2
  • 12
  • 12
  • Please fix the typo in your code. `datetime – Jota Sep 10 '16 at 02:39
  • `library(tidyverse) ; data_frame(datetime = mdy_hm(datetime)) %>% rownames_to_column('index') %>% group_by(date = date(datetime)) %>% filter(datetime == max(datetime))` – alistaire Sep 10 '16 at 02:51

2 Answers2

2

Convert those character values to datetimes and then split by date (day-month). Within each date, pick the last value using 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"
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • That gives me an error Error in which.max(d) : (list) object cannot be coerced to type 'double' In addition: Warning message: In split.default(datetime, format(dt, "%m-%d")) : data length is not a multiple of split variable – user1434997 Sep 10 '16 at 02:27
  • 1
    If you do not convert to an R datetime value first you might get the error but I just re-ran _all_ of that code and there is no error. – IRTFM Sep 10 '16 at 03:56
0

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", "12/23/2012 19:23", "11/24/2015 7:13")

Code:

splitter <- strftime(strptime(datetime,"%m/%d/%Y %H:%M"), "%m/%d/%Y")    
lapply(split(datetime, splitter), 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, splitter), function(x) {
    val_index <- match(x[length(x)], datetime)
    c(val_index,datetime[val_index])
})

Output1:

$`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

Output2:

$`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"
Abdou
  • 12,931
  • 4
  • 39
  • 42