作者goodparent1 (豆芽先生)
看板R_Language
标题[问题] error in xts
时间Thu Nov 7 18:44:24 2013
[问题类型]:
程式谘询(我想用R 做某件事情,但是我不知道要怎麽用R 写出来)
[软体熟悉度]:
新手(没写过程式,R 是我的第一次)
[问题叙述]:
x<-read.table("s.txt",header=T,row.names=c("time"))
head(x)
adjprc.10000 adjprc.10001
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
这是我的资料形式,在跑程式时遇到一些问题,大概R是无法判断我的时间格式,
不知道怎麽转换成R读得懂的格式,上网找了几天资讯还是解决不了。
我是R新手,还请大家教我一下,谢谢。
structure(list(adjprc.10000 = c(-4.375, -3.25, -4.4375, -4, -3.10938,
-3.09375, -2.84375, -1.09375, -1.03125, -0.78125, -0.82813, -0.51563,
-0.40625, -0.40625, -0.25, -0.23438, -0.21875, 0, NA, NA, NA,
NA, NA, NA), adjprc.10001 = c(-2.04167, -2.08333, -2.10417, -2.125,
-2.10417, -2.04167, -2.02083, -2.16667, 2.125, 2.20833, 2.33333,
2.33333, 2.25, 2.08333, 2.125, -2.04167, -1.89583, 1.95833, 2,
2.16667, 2.08333, 2.125, 2.0625, 1.95833)), .Names = c("adjprc.10000",
"adjprc.10001"), class = "data.frame", row.names = c("2000-01-31",
"2000-02-29", "2000-03-31", "2000-04-30", "2000-05-31", "2000-06-30",
"2000-07-31", "2000-08-31", "2000-09-30", "2000-10-31", "2000-11-30",
"2000-12-31", "2001-01-31", "2001-02-28", "2001-03-31", "2001-04-30",
"2001-05-31", "2001-06-30", "2001-07-31", "2001-08-31", "2001-09-30",
"2001-10-31", "2001-11-30", "2001-12-31"))
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 134.208.22.38
1F:→ celestialgod:你的时间列式ROW NAMES吧... 11/07 19:30
2F:→ Wush978:try `strptime(row.names(x))` 11/07 21:58
>strptime(row.names(time),"%y-%m-%d")
出现character(0),跑code一样出现
错误在xts: order.by requires an appropriate time-based object
应该是要转换成POSIXt或POSIXct,因为当初从网上抓资料下来有一行code是
x=c("POSIXt","POSIXct"),网上抓下来的资料就可以直接跑没问题。
不知道为什麽存档後再读入档案就没办法跑。
但是我用as.POSIXct(row.names=("time"))
会出现错误:缺少引数 "x",也没有预设值
不知道怎麽改,问题出在哪。
3F:→ Wush978:可以po一下`dump(x, file="x.R")`的结果吗? 11/08 13:28
4F:→ Wush978:sorry, 应该是dump("x", file="x.R") 11/08 13:29
po在上面
5F:→ celestialgod:x<-read.table("s.txt",header=T); 11/08 15:13
6F:→ celestialgod:time = strptime(row.names(x), "%Y-%m-%d") 11/08 15:13
7F:→ celestialgod:原po,你INPUT的变数是错的... 11/08 15:13
但是改过再跑一样出现error,直接用R从网路上抓的资料就能跑。不知道问题出在哪
8F:→ celestialgod:我的可以 11/08 20:04
我是指改过虽然成功了,但跑我的code出现同样的error。
9F:→ celestialgod:你的资料要不要PO一小段上来看看 11/08 22:29
你是说原始档吗?
10F:推 Wush978:"%Y", 不是"%y". 我用strptime(row.names(x),"%Y-%m-%d") 11/08 23:30
我是用这个code下去跑的,虽然没显示error 但是之後跑我的code出现同样的error in xts
11F:→ celestialgod:%Y有成功,%y失败 11/08 23:54
12F:→ Wush978:Hmm... 之後什麽code? 11/09 00:42
http://ideone.com/prg5vf
跑到最後一行时会出现error in xts(x = vec.ret, order.by = index(xts.ret)):
order.by requires an appropriate time-based object
http://stackoverflow.com/questions/9757430/xts-error-order-by-requires-an-appropriate-time-based-object
这是我参考外国网站有人跟我遇到一样的问题,可是还是没办法解决。
13F:→ celestialgod:原PO把你的资料取前三行跟rowname, colname列出来 11/09 01:58
14F:→ celestialgod:可能前十列就够了 11/09 01:58
15F:→ celestialgod:这样才能知道为什麽套到你的资料无法使用 11/09 01:58
https://www.dropbox.com/s/bq5jw1tolvscjlv/s.txt
这是我的测试档
16F:→ celestialgod:你的资料第一行才是时间,不是rowname, 11/10 04:05
17F:→ celestialgod:因此,应该是用time = strptime(x[,1], "%Y-%m-%d") 11/10 04:05
可是strptime这个函数是把时间转换成y-m-d格式,我的资料本身已经是y-m-d格式了。
18F:→ celestialgod:抱歉,我误会了,可是我用rowname也是有成功的 11/10 04:07
19F:→ celestialgod:你的CODE恩..太多,实在没时间帮你看... 11/10 04:08
那请问有其他发问平台,可以让我问问题吗?
因为我这问题自己解决不了,可是又一定要解决。
这是我dump用R抓下来的资料,
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")))
差别应该是class不同,当初储存档案时把他改成data.frame
不知道在怎麽把档案的class从data.frame转换成xts应该才是问题所在。
※ 编辑: goodparent1 来自: 114.37.240.23 (11/10 21:47)
20F:推 Wush978:你可以参考 ?xts 中的范例 11/10 23:32
21F:→ Wush978:里面有把sample_matrix转换成xts的过程 11/10 23:32
22F:→ Wush978:发问平台可以参考置底文章,里面有列一些 11/10 23:33
23F:→ Wush978:我今天弄好Rstudio後看了一下,问题大概在monthly.returns 11/11 11:24
24F:推 Wush978:你可能ROC函数要研究一下 11/11 11:25
25F:→ Wush978:你的问题其实满复杂的,你的问法也要让别人花很多时间找真 11/11 11:25
26F:→ Wush978:正的问题,所以除非运气好遇到刚好有相关经验的网友,否则 11/11 11:25
27F:→ Wush978:找到你可以接受答案可能机会不大... 11/11 11:26
29F:→ Wush978:btw, 最後一行改成xts.ret = as.xts(monthly.returns)可跑 11/11 11:32
30F:→ Wush978:但是我完全不知道跑出来的答案是不是对的,所以请自己小心 11/11 11:33
31F:推 celestialgod:版主用心! 11/11 12:15
真的非常谢谢两位帮忙,真的感恩。我在自己研究进修一下。
※ 编辑: goodparent1 来自: 134.208.22.38 (11/11 13:50)