0

I have a list of dates for 1985 (hourly): for each day there is 24 hours. My object is fg, here's an example:

fg

 "1985-01-01 00:30:00 GMT" to "1985-01-01 22:30:00 GMT" then NA # (NA here should be "1985-01-01 23:30:00 GMT")

 "1985-01-02 00:30:00 GMT" to "1985-01-02 22:30:00 GMT" then NA # (NA here should be "1985-01-02 23:30:00 GMT ")

 "1985-01-03 00:30:00 GMT" to "1985-01-03 22:30:00 GMT" then NA

 "1985-01-04 00:30:00 GMT" to "1985-01-04 22:30:00 GMT" then NA

 str(fg)
 POSIXct[1:8760], format: "1985-01-01 00:30:00" "1985-01-01 01:30:00" 

 class(fg)
 [1] "POSIXct" "POSIXt"

the last hour 23:30:00 is missing in each day so I want here to replace NA with last hour of that day.

Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
Barry
  • 739
  • 1
  • 8
  • 29
  • "24 times" means what? Like 24 hours? – Andrew Lank Jul 21 '15 at 17:02
  • 2
    Is your `fg` a sequence from 1985-01-01 00:30:00 in hourly increments with all of the 23:30s missing? How did you generate the sequence? Can you just regenerate the whole thing with `seq(from=as.POSIXct('1985-01-01 00:30:00',tz="GMT"), to = as.POSIXct('1985-12-31 23:30:00',tz="GMT"),by=60*60)` – Dean MacGregor Jul 21 '15 at 17:39

1 Answers1

1

You should really give us a reproducible example (How to make a great R reproducible example?). But the following seems to work on my example of your data

library(lubridate)
fg <- c(ymd("2015-12-01"), ymd("2015-12-02"), NA)
as.POSIXct(ifelse(is.na(fg), c(as.POSIXct(NA), fg[-length(fg)]) + hours(1), fg), origin="1970-01-01")

Note: I am using lubridate to create the example, but also for the hours function.

Note 2: not sure where the need for the outside as.POSIXct comes from, but it is needed.

Community
  • 1
  • 1
kasterma
  • 4,259
  • 1
  • 20
  • 27