只需要 R 中日期时间对象的时间部分 [英] Want only the time portion of a date-time object in R

查看:33
本文介绍了只需要 R 中日期时间对象的时间部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 R 中有一个时间向量,all_symbols$Time,我试图找出如何获得时间(或将时间转换为字符串而不丢失信息).我用

I have a vector of times in R, all_symbols$Time and I am trying to find out how to get JUST the times (or convert the times to strings without losing information). I use

strptime(all_symbol$Time[j], format="%H:%M:%S")

出于某种原因假设日期是今天并返回

which for some reason assumes the date is today and returns

[1] "2013-10-18 09:34:16"

[1] "2013-10-18 09:34:16"

R 中的日期和时间格式很烦人.我只是想在不添加太多包的情况下争取时间(真的任何 - 我在无法安装库的学校计算机上).

Date and time formatting in R is quite annoying. I am trying to get the time only without adding too many packages (really any--I am on a school computer where I cannot install libraries).

推荐答案

使用 strptime 后,您将必然获得一个日期时间对象,格式字符串中没有日期的默认行为是假设今天的日期.如果您不喜欢这样,则需要在前面添加一个字符串,该字符串是您选择的日期.

Once you use strptime you will of necessity get a date-time object and the default behavior for no date in the format string is to assume today's date. If you don't like that you will need to prepend a string that is the date of your choice.

@James 的建议和我要建议的一样:

@James' suggestion is equivalent to what I was going to suggest:

format(all_symbol$Time[j], format="%H:%M:%S")

我所知道的唯一具有时间类(即没有关联日期值的时间)的包是 package:chron.但是我发现使用 format 作为从 POSIXt 对象输出字符值的一种方式非常适合需要因子输入的函数.

The only package I know of that has time classes (i.e time of day with no associated date value) is package:chron. However I find that using format as a way to output character values from POSIXt objects lends itself well to functions that require factor input.

这篇关于只需要 R 中日期时间对象的时间部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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