如何在R中的年,月,日和日期之间双向转换? [英] How to convert in both directions between year,month,day and dates in R?

查看:151
本文介绍了如何在R中的年,月,日和日期之间双向转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在R中的年,月,日和日期之间进行转换?

How to convert between year,month,day and dates in R?

我知道可以通过字符串完成此操作,但我希望避免转换为字符串,部分是因为可能会对性能造成影响?,部分是因为我担心区域化问题,世界上有些地区使用年-月-日,而有些地区使用年-月-月。

I know one can do this via strings, but I would prefer to avoid converting to strings, partly because maybe there is a performance hit?, and partly because I worry about regionalization issues, where some of the world uses "year-month-day" and some uses "year-day-month".

似乎ISODate提供了year,month,day-> DateTime的方向,尽管它首先将数字转换为字符串,所以如果有一种方法不能通过字符串那么我更喜欢。

It looks like ISODate provides the direction year,month,day -> DateTime , although it does first converts the number to a string, so if there is a way that doesn't go via a string then I prefer.

我找不到从日期时间到数值相反的东西吗?我宁愿不需要使用strsplit或类似的东西。

I couldn't find anything that goes the other way, from datetimes to numerical values? I would prefer not needing to use strsplit or things like that.

编辑:为清楚起见,我所拥有的是一个看起来像这样的数据框:

just to be clear, what I have is, a data frame which looks like:

year month day hour somevalue
2004 1     1   1   1515353
2004 1     1   2   3513535
....

我希望能够自由转换为以下格式:

I want to be able to freely convert to this format:

time(hour units) somevalue
1             1515353
2             3513535
....

...,也可以再次返回。

... and also be able to go back again.

编辑:使用

to clear up some confusion on what 'time' (hour units) means, ultimately what I did was, and using information from How to find the difference between two dates in hours in R?:

前进方向:

lh$time <- as.numeric( difftime(ISOdate(lh$year,lh$month,lh$day,lh$hour), ISOdate(2004,1,1,0), units="hours"))
lh$year <- NULL; lh$month <- NULL; lh$day <- NULL; lh$hour <- NULL

后退方向:

...好吧,我还没有倒退,但是我想像的是:

... well, I didnt do backwards yet, but I imagine something like:


  • 在lh $ time之外创建difftime对象(以某种方式...)

  • 将ISOdate(2004,1,1,0)添加到difftime对象

  • 使用以下解决方案之一获取年,月,日,小时倒计时

我想将来,我可以问一下确切的问题我正在尝试解决,但是我试图将我的特定问题分解为通用的可重用问题,但这也许是一个错误吗?

I suppose in the future, I could ask the exact problem I'm trying to solve, but I was trying to factorize my specific problem into generic reusable questions, but maybe that was a mistake?

推荐答案

因为有很多方法可以从文件,数据库等中传递日期,并且由于您提到只是以不同的顺序或使用不同的分隔符(代表输入的 date作为字符串是一种方便且有用的解决方案。 R并不将实际日期作为字符串保存,并且您不需要将它们作为字符串来处理即可使用。

Because there are so many ways in which a date can be passed in from files, databases etc and for the reason you mention of just being written in different orders or with different separators, representing the inputted date as a character string is a convenient and useful solution. R doesn't hold the actual dates as strings and you don't need to process them as strings to work with them.

内部R正在使用操作系统来完成这些事情都是以标准方式进行的。您根本不需要操纵字符串-只需将某些内容从字符转换为等效的数字即可。例如,将两个操作(向前和向后)都包装在可以部署的简单函数中非常容易。

Internally R is using the operating system to do these things in a standard way. You don't need to manipulate strings at all - just perhaps convert some things from character to their numerical equivalent. For example, it is quite easy to wrap up both operations (forwards and backwards) in simple functions you can deploy.

toDate <- function(year, month, day) {
    ISOdate(year, month, day)
}

toNumerics <- function(Date) {
    stopifnot(inherits(Date, c("Date", "POSIXt")))
    day <- as.numeric(strftime(Date, format = "%d"))
    month <- as.numeric(strftime(Date, format = "%m"))
    year <- as.numeric(strftime(Date, format = "%Y"))
    list(year = year, month = month, day = day)
}

我放弃了对 strptime(),然后在分隔符上进行拆分,因为您不喜欢这种操作。

I forego the a single call to strptime() and subsequent splitting on a separation character because you don't like that kind of manipulation.

> toDate(2004, 12, 21)
[1] "2004-12-21 12:00:00 GMT"
> toNumerics(toDate(2004, 12, 21))
$year
[1] 2004

$month
[1] 12

$day
[1] 21

内部R的日期时间代码运行良好且经过了良好测试如果由于时区问题等原因在地方有点复杂,那么它会很健壮。我发现 toNumerics()中使用的习惯用法比将日期时间作为列表并记住哪些元素更直观基于0。比起尝试避免字符串转换等,在提供的功能上构建似乎更容易。

Internally R's datetime code works well and is well tested and robust if a bit complex in places because of timezone issues etc. I find the idiom used in toNumerics() more intuitive than having a date time as a list and remembering which elements are 0-based. Building on the functionality provided would seem easier than trying to avoid string conversions etc.

这篇关于如何在R中的年,月,日和日期之间双向转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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