R以yyyymmdd格式读取日期 [英] R read dates in format yyyymmdd

查看:179
本文介绍了R以yyyymmdd格式读取日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,第一列有yyyymmdd格式的日期字段。我尝试将这些日期转换为R格式:

I have a dataset, the first column has the date field with yyyymmdd formats. I tried to convert these dates to R format using:

    yield.usd$Fecha <- as.Date(yield.usd$Fecha, format='%Y%m%d')

,输出是: p>

and the output is:

Error in as.Date.numeric(yield.usd$Fecha, format = "%Y%m%d") : 'origin' must be supplied

然后我尝试:

    yield.dates <- as.Date(yield.usd[1], format='%Y%m%d')

输出如下:

Error in as.Date.default(yield.usd[1], format = "%Y%m%d") : do not know how to convert 'yield.usd[1]' to class "Date"

如何使R读取这些日期?

How can I make R read those dates?

dput(head(yield.usd))如下:

structure(list(Fecha = c(20120815L, 20120815L, 20120815L, 20120815L, 
20120815L, 20120815L), Plazo = 1:6, Soberana = c(0.001529738, 
0.001558628, 0.001587518, 0.001616408, 0.001645299, 0.001674189
), AAA = c(0.009642716, 0.009671607, 0.009700497, 0.009729387, 
0.009758277, 0.009787168), AA. = c(0.017483959, 0.01751285, 0.01754174, 
0.01757063, 0.01759952, 0.017628411), AA = c(0.017762383, 0.017791273, 
0.017820163, 0.017849053, 0.017877944, 0.017906834), AA..1 = c(0.018207843, 
0.018236733, 0.018265624, 0.018294514, 0.018323404, 0.018352294
), A = c(0.036340293, 0.036369183, 0.036398073, 0.036426964, 
0.036455854, 0.036484744)), .Names = c("Fecha", "Plazo", "Soberana", 
"AAA", "AA.", "AA", "AA..1", "A"), row.names = c(NA, 6L), class = "data.frame")   


推荐答案

您的日期是数字格式,所以R认为它代表一个时间戳(在这种情况下,它需要时间戳的起源,通常1970年1月1日)。你想要的是字符串的理解,所以你应该首先将你的数字转换成字符:

Your dates are in number format, so R thinks it represents a timestamp (in such case it needs origin of timestamp, usually 1st January 1970). What you want is comprehension from strings, so you should convert your numbers to characters first:

as.Date(as.character(yield.usd$Fecha), format='%Y%m%d')

这篇关于R以yyyymmdd格式读取日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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