不带日期但不作为班级角色存储时间 [英] Storing time without date but not as class character

查看:49
本文介绍了不带日期但不作为班级角色存储时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的类字符数据框中有一个local.time列,其中包含这样的元素:

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)

结果是:

[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.

我想从a $ local.time中删除日期和时区,但将其保留为时间格式,这样就可以保持时间顺序,即18:57是第19个小时和一天中的第57分钟,等等。

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.

如果我使用


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

该类更改为character!什么是正确的方法?

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

推荐答案

chron 包中的 times类可能是对您有帮助。从类似于您到目前为止的内容开始:

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)

然后我们可以将 times 函数与 format

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"

这篇关于不带日期但不作为班级角色存储时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆