遇到一個問題,再跑code時如果用R直接從網上抓下來的資料可以順利跑成功
可是如果把他存檔(write)再讀入(read)就會跑失敗。
下面是我data樣式
[data] x<-read.table("s.txt",header=T,row.names=c("time"))
head(x)
adjprc.XLY adjprc.XLP .......
2000-01-31 -4.37500 -2.04167
2000-02-29 -3.25000 -2.08333
2000-03-31 -4.43750 -2.10417
2000-04-30 -4.00000 -2.12500
2000-05-31 -3.10938 -2.10417
2000-06-30 -3.09375 -2.04167
.
.[/data]
我分別dump兩種資料看資料的架構如下:
用R從網路上抓的資料架構:
[data] structure(c(23.59, 22.28, 25.39, 24.86, 23.51, 22.24, 22.59,...), .Dim=c(167L,4L),
.Dimnames = list(NULL, c("XLY.Adjusted","XLP.Adjusted", "XLE.Adjusted","XLF.Adjusted")),
index = structure(c(949276800,951782400, 954460800, 957052800, 959731200,
962323200, 965001600,967680000, 970272000, 972950400, 975542400,... ),
tclass ="Date"), class = c("xts", "zoo"),
.indexCLASS = "Date", .indexFORMAT = "%Y-%m-%d",
tclass = "Date",tzone = "UTC", src = "yahoo",
updated = structure(1384070717.82835, class = c("POSIXct", "POSIXt"))) [/data]
在電腦裡讀入的資料架構:
[data] structure(list(adjprc.XLY = c(-4.375, -3.25, -4.4375,...),
adjprc.XLP = c(-2.04167, -2.08333, -2.10417,...3)), .Names = c("adjprc.XLY",
"adjprc.XLP"), class = "data.frame", row.names = c("2000-01-31",
"2000-02-29", "2000-03-31", "2000-04-30",...)) [/data]
問題應該是在class的不同,當出存檔時變成class=data.frame
不知道在怎麼改回去class = c("xts", "zoo") or class = c("POSIXct", "POSIXt")
如果有需要我會附上我的code跟data