Storing time without date but not as class character

落爺英雄遲暮 提交于 2019-12-07 12:03:01

问题


I have a local.time column in my data frame of class character containing elements like this :

> a$local.time
 [1] "1:30 AM"  "6:29 AM"  "6:59 AM"  "9:54 AM"  "10:14 AM" "10:34 AM" "12:54 PM" "1:15 PM"  "1:20 PM" 
 [10] "1:20 PM"  "2:15 PM"  "2:15 PM"  "4:23 AM"  "6:28 AM"  "2:45 PM"  "3:08 PM"  "3:23 PM"  "3:58 PM" 

I wanted to convert them from class character to time variables. So I used:

> as.POSIXct(a$local.time, tz = "", format = "%I:%M %p", usetz = FALSE)

This resulted in :

[1] "2014-10-31 01:30:00 EDT" "2014-10-31 06:29:00 EDT" "2014-10-31 06:59:00 EDT" "2014-10-31       09:54:00 EDT"
[5] "2014-10-31 10:14:00 EDT" "2014-10-31 10:34:00 EDT" "2014-10-31 12:54:00 EDT" "2014-10-31 13:15:00 EDT"

I have a date variable in a different column and the intention is to provide the capability of filtering by date and zooming on time bands to the minute in a dynamic dashboard.

I want to remove the date and time zone from a$local.time but keep it in a time format so the chronology is maintained i.e. 18:57 is the 19th hour and 57th minute of the day etc.

If I use

a$local.time <- format(a$local.time, "%Y-%m-%d %H:%M:%S", usetz = FALSE) a$local.time <- strftime(a$local.time, format = "%H:%m") ,

the class changes to character ! What's the right approach?


回答1:


The chron package has a "times" class that might be helpful for you. Starting with something similar to what you have so far:

x <- c("1:30 AM", "6:29 AM", "6:59 AM", "9:54 AM", "10:14 AM", "3:15 PM"))
a <- as.POSIXct(x, tz = "", format = "%I:%M %p", usetz = FALSE)

Then we can use the times function with format

library(chron)
(tms <- times(format(a, "%H:%M:%S")))
# [1] 01:30:00 06:29:00 06:59:00 09:54:00 10:14:00 15:15:00
attributes(tms)
# $format
# [1] "h:m:s"
#
# $class
# [1] "times"



回答2:


You can use the hms (hour-minute-second) series of functions in the lubridate package.

library(lubridate)

times = c("1:30 AM",  "6:29 AM",  "6:59 AM",  "9:54 AM", "2:45 PM")

I was hoping you could just do:

hm(times)
[1] "1H 30M 0S" "6H 29M 0S" "6H 59M 0S" "9H 54M 0S" "2H 45M 0S"

But notice that hm doesn't recognize the AM/PM distinction. So here's a more convoluted method that requires first using strptime, which does recognize AM/PM, and then putting the result in a form hm recognizes.

hm(paste0(hour(strptime(times, "%I:%M %p")),":",
          minute(strptime(times, "%I:%M %p"))))
[1] "1H 30M 0S"  "6H 29M 0S"  "6H 59M 0S"  "9H 54M 0S"  "14H 45M 0S"

There's probably a better way, but this seems to work.

UPDATE: To address your comment, you can use the hour and minute functions to get the hours and minutes (although I like @RichardScriven's answer better). For example:

hour(times)
[1]  1  6  6  9 14

mean(hour(times) + minute(times)/60)
[1] 7.923333


来源:https://stackoverflow.com/questions/26680499/storing-time-without-date-but-not-as-class-character

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!