将整数转换为类日期 [英] Convert integer to class Date
本文介绍了将整数转换为类日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个整数,我想将其转换为 Date
类.我假设我首先需要将其转换为字符串,但如何转换?
我的尝试:
v <- 20081101日期 <- as.Date(v, format("%Y%m%d"))
<块引用>
charToDate(x) 中的错误:字符串不在标准中格式明确
使用 paste()
有效,但这真的是进行转换的正确方法吗?
date <- as.Date(paste(v), format("%Y%m%d"))日期[1] 《2008-11-01》班级(日期)# [1]日期"
解决方案
as.character()
将是通用的方式,而不是使用 paste()
作为它的一面效果
>v <- 20081101>日期 <- as.Date(as.character(v), format = "%Y%m%d")>日期[1] 《2008-11-01》
(我认为这是一个简单的例子,类似这样:
v <- "20081101"
不可能?)
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?
My attempt:
v <- 20081101
date <- as.Date(v, format("%Y%m%d"))
Error in charToDate(x) : character string is not in a standard unambiguous format
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()
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"
isn't possible?)
这篇关于将整数转换为类日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文