简体   繁体   English

R timeDate缩短了强制时间

[英]R timeDate drops time in coersion

I have dates of the format: "2/9/2016 21:16" 我有以下格式的日期:“ 2/9/2016 21:16”

When I attempt to coerce them to a timeDate, I receive the result: [1] [2016-02-03] 当我尝试将其强制为timeDate时,收到以下结果:[1] [2016-02-03]

I would prefer to not have to write my own string manipulation, but I can and already have, but there has to be a better way. 我希望不必编写自己的字符串操作,但我可以并且已经拥有,但是必须有更好的方法。 I have a dataframe and I am attempting to do the following: 我有一个数据框,并且尝试执行以下操作:

restData2 <- restData %>% 
  mutate(year = year(as.timeDate(Date)),
     month = month(as.timeDate(Date)),
     day = day(as.timeDate(Date)),
     timeCategory = converToTimeCategory(Date)
  )

Note, that day is not a function in timeDate either. 注意,那一天也不是timeDate中的函数。 Day of Week and Day of year exist, I need Day of Month. 存在星期几和一年中的某天,我需要每月的某天。

The data exists in a data frame. 数据存在于数据帧中。 The data is basic transaction data. 该数据是基本交易数据。

David, you are confused. 大卫,你很困惑。 R differentiates between internal representation and actual formated display . R区分内部表示形式和实际格式显示 For all types. 对于所有类型。

And there is (once again) no need for timeDate, lubridate, or any other wrapper: 而且(再次)不需要timeDate,lubridate或任何其他包装器:

R> intxt <- c("2/9/2016 21:16", "2/11/2016 22:23")
R> parsed <- as.POSIXct(intxt, format="%d/%m/%Y %H:%M")
R> parsed
[1] "2016-09-02 21:16:00 CDT" "2016-11-02 22:23:00 CDT"
R> format(parsed, "%d %b %Y at %H:%M")
[1] "02 Sep 2016 at 21:16" "02 Nov 2016 at 22:23"
R> 

Here we parse a datetime object into the standard POSIXct , specifying a format. 在这里,我们将日期时间对象解析为标准POSIXct ,并指定一种格式。 Which can be day-month or month-day; 可以是日月或月日; here I picked the former. 在这里,我选择了前者。

Given the parsed object, I first show the default display, and then a custom format string. 给定已parsed对象,我首先显示默认显示,然后显示自定义格式字符串。

Lastly, if you must, you can also convert to timeDate : 最后,如果必须的话,还可以转换为timeDate

R> library(timeDate)
R> as.timeDate(parsed)
GMT
[1] [2016-09-03 02:16:00] [2016-11-03 03:23:00]
R> 

Not the timezone adjustment from my local (Central) time. 不是我的本地(中央)时间的时区调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM