在阅读Excel文件时,用5个Didgits Integers作为日期格式解析一列
对于excel文件(从):
df <- openxlsx::read.xlsx('sample_data.xlsx', sheet='Sheet1', colNames=TRUE)
df
输出:
date value
1 43861 5.70
2 43890 -13.89
3 43921 -49.68
4 43951 -62.81
我尝试将日期列转换为正常日期格式:
> df %>%
+ mutate(date=as.Date(date, origin = "1970-01-01"))
date value
1 2090-02-01 5.70
2 2090-03-02 -13.89
3 2090-04-02 -49.68
4 2090-05-02 -62.81
> df %>%
+ mutate(date=as.Date(date, origin = "1910-01-01"))
date value
1 2030-02-01 5.70
2 2030-03-02 -13.89
3 2030-04-02 -49.68
4 2030-05-02 -62.81
我用1970-01-01
和1910-01-01进行了测试
作为onement
参数的值,输出中的日期似乎不正确(43861
已转换为2090-02-02-01
和2030-02-01
,应为2020-01-31
)。
For an excel file (download from here):
df <- openxlsx::read.xlsx('sample_data.xlsx', sheet='Sheet1', colNames=TRUE)
df
Output:
date value
1 43861 5.70
2 43890 -13.89
3 43921 -49.68
4 43951 -62.81
I try to convert date column to a normal date format:
> df %>%
+ mutate(date=as.Date(date, origin = "1970-01-01"))
date value
1 2090-02-01 5.70
2 2090-03-02 -13.89
3 2090-04-02 -49.68
4 2090-05-02 -62.81
> df %>%
+ mutate(date=as.Date(date, origin = "1910-01-01"))
date value
1 2030-02-01 5.70
2 2030-03-02 -13.89
3 2030-04-02 -49.68
4 2030-05-02 -62.81
I tested with 1970-01-01
and 1910-01-01
as value for origin
parameter, the dates in the output seems incorrect (43861
has been convert to 2090-02-01
and 2030-02-01
, which should be 2020-01-31
).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Origin
必须是as.date
调用。origin
has to be inside theas.Date
call.