问题描述
我有一个数字excel日期的向量,即
I have a vector of numeric excel dates i.e.
date<-c(42963,42994,42903,42933,42964)
使用 janitor中的
包和<$ c $ zoo 包中的 excel_numeric_to_date
函数时,输出是期望的 as.yearmon
函数
The output am I expecting when using excel_numeric_to_date
function from janitor
package and as.yearmon
function from zoo
package
as.yearmon(excel_numeric_to_date(date))[1] 2016年8月 2016年9月 2017年6月 2017年7月 2017年8月
。
但是,日期
向量的第一个元素到元素的转换是不正确的。 实际结果是:
However, the conversion for the first to elements of the date
vector are incorrect. The actual result are:
as.yearmon(excel_numeric_to_date(date))[1] 2017年8月 2017年9月 2017年6月 2017年7月 2017年8月
我尝试使用其他选项(现代
和 mac 2011年以前的版本
)作为 excel_numeric_to_date
中的 date_system
自变量,但没有
I have tried using different option(modern
and mac pre-2011
) for the date_system
argument in the excel_numeric_to_date
but it does not help either
excel版本是2010
The excel version is 2010
推荐答案
您可以简单地使用 as.Date
并指定原点,即
You can simply use as.Date
and specify the origin, i.e.
as.Date(date, origin="1899-12-30")
#[1] "2017-08-16" "2017-09-16" "2017-06-17" "2017-07-17" "2017-08-17"
#or format it to your liking,
format(as.Date(date, origin="1899-12-30"), '%b %Y')
#[1] "Aug 2017" "Sep 2017" "Jun 2017" "Jul 2017" "Aug 2017"
此提供了很多有关此问题的信息。
This link gives quite a bit of information on this matter.
这篇关于将Excel数字转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!