如何在R中将Excel日期格式转换为正确的日期 [英] How to convert Excel date format to proper date in R

查看:523
本文介绍了如何在R中将Excel日期格式转换为正确的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个csv,不幸的是它使用42705的数字格式记录了日期时间,尽管它应该是2016年1月12日.

I'm working with a csv which unfortunately has logged datetimes using the number format of 42705 although it should be 01/12/2016.

我想使用lubridate或其他软件包将其转换为R中正确的格式.有可以处理的功能吗?

I'd like to convert it to the right format in R using lubridate or some other package. Is there a function that will handle it?

推荐答案

您无需为此使用lubridate,基本函数as.Date可以很好地处理这种类型的转换.诀窍是必须提供原点,在Excel中是1899年12月30日.

You don't need to use lubridate for this, the base function as.Date handles this type of conversion nicely. The trick is that you have to provide the origin, which in Excel is December 30, 1899.

as.Date(42705, origin = "1899-12-30")
# [1] "2016-12-01"

如果要保留列类型,可以尝试使用readxl包中的read_excel函数.这样一来,您就可以加载保留了数字格式的XLS或XLSX文件.

If you want to preserve your column types, you can try using the read_excel function from the readxl package. That lets you load an XLS or XLSX file with the number formatting preserved.

这篇关于如何在R中将Excel日期格式转换为正确的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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