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

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

问题描述

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

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.

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

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