将整数转换为日期类 [英] Convert integer to class Date
本文介绍了将整数转换为日期类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个整数,想要转换为日期 Date
。我假设我首先需要将其转换为字符串,但是如何?
I have an integer which I want to convert to class Date
. I assume I first need to convert it to a string, but how?
我的尝试:
v <- 20081101
date <- as.Date(v, format("%Y%m%d"))
charToDate(x)中的错误:字符串不是标准的
明确格式
Error in charToDate(x) : character string is not in a standard unambiguous format
使用 paste()
可行,但这真的是进行转换的正确方法吗?
Using paste()
works, but is that really the correct way to do the conversion?
date <- as.Date(paste(v), format("%Y%m%d"))
date
[1] "2008-11-01"
class(date)
# [1] "Date"
推荐答案
as.character()
是常规方法,而不是使用 paste()
产生副作用
as.character()
would be the general way rather than use paste()
for its side effect
> v <- 20081101
> date <- as.Date(as.character(v), format = "%Y%m%d")
> date
[1] "2008-11-01"
(我想这是一个一个简单的例子,像这样:
(I presume this is a simple example and something like this:
v<- 20081101
不可能吗?)
这篇关于将整数转换为日期类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文