我有一个数据集,第一列具有 yyyymmdd 格式的日期字段。我尝试使用以下方法将这些日期转换为 R 格式:
yield.usd$Fecha <- as.Date(yield.usd$Fecha, format='%Y%m%d')
输出是:
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 读取这些日期?
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")