How to create only the temporary part of the timestamp, including the date?

So, I have a set of timestamps like this:

datetime<-c("2011-09-28 03:33:00", "2011-08-24 13:41:00", "2011-09-19 16:14:00", "2011-08-18 11:01:00", "2011-09-17 06:35:00", "2011-08-15 12:48:00") 

I want to make a histogram only once. What I did was split the column in space to only get time, and then convert back to a POSIXct object so qplot could build it:

 library(ggplot2, stringr) qplot(as.POSIXct(strptime((str_split_fixed(as.character(time), " ", 2)[,2]), "%H:%M:%S"))) 

However, the output of as.POSIXct(strptime((str_split_fixed(as.character(datetime), " ", 2)[,2]), "%H:%M:%S")) is

 "2011-10-04 03:33:00 PDT" "2011-10-04 13:41:00 PDT" "2011-10-04 16:14:00 PDT" "2011-10-04 11:01:00 PDT" "2011-10-04 06:35:00 PDT" "2011-10-04 12:48:00 PDT" 

qplot shows what I want, but it seems like a confusing hack for me. Is there really a better way to do this? I could transform into an era and speak, but I tried to avoid having to do this as an extra step.

The bigger question is: "How can I control the output of strptime?"

+11
timestamp r ggplot2 strptime
Oct 05 2018-11-11T00: 00Z
source share
2 answers

How about this approach?

 require("ggplot2") dtstring <- c( "2011-09-28 03:33:00", "2011-08-24 13:41:00", "2011-09-19 16:14:00", "2011-08-18 11:01:00", "2011-09-17 06:35:00", "2011-08-15 12:48:00" ) dtPOSIXct <- as.POSIXct(dtstring) # extract time of 'date+time' (POSIXct) in hours as numeric dtTime <- as.numeric(dtPOSIXct - trunc(dtPOSIXct, "days")) p <- qplot(dtTime) + xlab("Time slot") + scale_x_datetime(format = "%S:00") print(p) 

The calculation, dtPOSIXct - trunc(dtPOSIXct, "days") , retrieves the time for objects of the POSIXct class in hours.

plot (p)

For ggplot2-0.9.1 :

 require("ggplot2") require("scales") dtstring <- c( "2011-09-28 03:33:00", "2011-08-24 13:41:00", "2011-09-19 16:14:00", "2011-08-18 11:01:00", "2011-09-17 06:35:00", "2011-08-15 12:48:00" ) dtPOSIXct <- as.POSIXct(dtstring) # extract time of 'date+time' (POSIXct) in hours as numeric dtTime <- as.numeric(dtPOSIXct - trunc(dtPOSIXct, "days")) p <- qplot(dtTime) + xlab("Time slot") + scale_x_datetime(labels = date_format("%S:00")) print(p) 

For ggplot2-0.9.3.1 :

 require("ggplot2") require("scales") dtstring <- c( "2011-09-28 03:33:00", "2011-08-24 13:41:00", "2011-09-19 16:14:00", "2011-08-18 11:01:00", "2011-09-17 06:35:00", "2011-08-15 12:48:00" ) dtPOSIXct <- as.POSIXct(dtstring) # extract time of 'date+time' (POSIXct) in hours as numeric dtTime <- as.numeric(dtPOSIXct - trunc(dtPOSIXct, "days")) class(dtTime) <- "POSIXct" p <- qplot(dtTime) + xlab("Time slot") + scale_x_datetime(labels = date_format("%S:00")) print(p) 
+13
Oct 05 2018-11-11T00:
source share
β€” -

Just use the basic tools as they were intended:

 dtstring <- c("2011-09-28 03:33:00", "2011-08-24 13:41:00", "2011-09-19 16:14:00", "2011-08-18 11:01:00", "2011-09-17 06:35:00", "2011-08-15 12:48:00") datetime <- as.POSIXct(dtstring) library(ggplot2) qplot(datetime) 

The format of your lines is the default for parsing with as.POSIXct , see ?strptime for more information or if you have anything else besides this format.

If you want to get a specific string format from your date values, use format , as in

 format(datetime, "%d-%b") [1] "28-Sep" "24-Aug" "19-Sep" "18-Aug" "17-Sep" "15-Aug" 

Again, see ?strptime for details. If you really want to reset the time, you can use the Date class. Just keep in mind that dates or dates are necessary for their complete structure, any other representation is just formatted text.

qplot (as.Date (DateTime))

+4
Oct 05 2018-11-11T00:
source share



All Articles