Save only hour: minute: second from object "POSIXlt" "POSIXt"

I am doing a plot in R (a graph of three days of a time series in one plot). I have "POSIXlt" "POSIXt", and I need to save only time (hour, minutes ...) without a year, month, day.

"2004-09-08 13:50:00 GMT" ---> 13:50:00
"2004-09-08 14:00:00 GMT" ---> 14:00:00
"2004-09-08 14:10:00 GMT" ---> 14:10:00
"2004-09-08 14:20:00 GMT" ---> 14:20:00
"2004-09-08 14:30:00 GMT" ---> 14:30:00

Is it possible?

I was able to make sure that all the elements in the vector have the same year / month / day. This works for my plot, but I do not think this is a suitable solution.

"2004-09-08 13:50:00 GMT" ---> "2014-10-19 13:50:00 GMT"
"2004-09-08 14:00:00 GMT" ---> "2014-10-19 14:00:00 GMT"
"2004-09-08 14:10:00 GMT" ---> "2014-10-19 14:10:00 GMT"
"2004-09-08 14:20:00 GMT" ---> "2014-10-19 14:20:00 GMT"
"2004-09-08 14:30:00 GMT" ---> "2014-10-19 14:30:00 GMT"

thank

+4
source share
1 answer

Suppose we have POSIXct values x.

library(chron)

# input
y <- 1:5
x <- as.POSIXct(c("2004-09-08 13:50:00", "2004-09-08 14:00:00", "2004-09-08 14:10:00",
"2004-09-08 14:20:00", "2004-09-08 14:30:00"))

1) Convert them to a chron class "times"and write:

ti <- times(format(x, "%H:%M:%S"))
plot(y ~ ti)

2) , or you can do it using the zoo:

library(zoo)

z <- zoo(y, x)

# convert index to "times" class and plot
zz <- z
time(zz) <- times(format(time(zz), "%H:%M:%S"))
plot(zz)

2a) ggplot2 autoplot.zoo :

library(ggplot2)
autoplot(zz) + scale_x_chron(format = "%H:%M")

2b) , . zoo/ggplot2/, chron, X:

library(scales)
autoplot(z) + scale_x_datetime(breaks = "10 min", labels = date_format("%H:%M"))
+7

All Articles