【发布时间】:2018-06-13 12:52:18
【问题描述】:
我是 R 的新手,在 xts 课程中难以将每日股票系列转换为每月系列。
我有一个关于股票代码 IOO 的股票数据从 Yahoo!Finance 下载到一个 .csv(逗号分隔)文件中,如下所示:
Date Open High Low Close Adjusted Close Volume
12/31/2012 63 63.95 63 63.95 56.11 87900
1/2/2013 64.94 65.19 64.62 65.08 57.09 77900
1/3/2013 64.94 64.98 64.63 64.68 56.74 36000
1/4/2013 64.70 65.13 64.63 65.12 57.13 49400
1/7/2013 64.83 64.91 64.61 64.88 56.93 102000
1/8/2013 64.65 64.76 64.37 64.58 56.66 31600
为了阅读,我在 R 中编写了以下内容,将其转换为 xts 并将每日转换为每月:
library(tseries)
library(xts)
library(PerformanceAnalytics)
IOO <- read.csv(file = “IOO.csv”, header = TRUE, sep = “,”)
IOO <- subset(IOO[, c(1,2,3,4,6)])
colnames(IOO) <- c(“Date”, “Open”, “High”, “Low”, “Close”)
IOO[,“Date”] <- as.Date(IOO[,“Date”], format = “%m / %d / %Y”)
IOO <- as.xts(IOO, order.by = as.Date(rownames(IOO), “%Y-%m-%d”), dateFormat = “POSIXct”, frequency = NULL, .RECLASS = FALSE)
IOO_monthly <- to.monthly(IOO, indexAt=‘yearmon’, drop.time = TRUE, name = NULL)
to.period(x, “months”, indexAt = indexAt, name = name, ...) 中的错误: 不支持的类型
IOO_monthly <- to.period(IOO, period = “months”, indexAt=‘yearmon’, name = NULL)
to.period(IOO, period = “months”, indexAt = “yearmon”, name = NULL) 中的错误: 不支持的类型
IOO_monthly <- to.period(IOO, period = “months”, indexAt= NULL, name = NULL)
to.period(IOO, period = “months”, indexAt = NULL, name = NULL) 中的错误: 不支持的类型
我在to.period 和to.monthly 中尝试了许多其他参数组合,但没有成功。
提前感谢您的帮助。
【问题讨论】:
-
你能输入你的数据吗?
-
@suchait 我不知道怎么做
-
当您在 r 中获取数据时,只需执行
dput(data)。并且,分享它的输出。那将是一个可重现的例子。你应该得到以structure开头的东西。 -
这不完整。它没有生成任何数据框。
-
structure(list(Date = structure(c(395L, 32L), .Label = c("1/10/2013", "1/10/2014", "1/10/2017 ", "1/11/2013", "1/11/2016", "1/11/2017"14100L, 21900L, 24700L, 207700L, 14100L, 28600L)), .Names = c("Date", "Open ", "High", "Low", "Close", "Adj.Close", "Volume"), class= "data.frame", row.names = c(NA, -1260L)) #前2个和最后2行看起来是这样的,否则数据很长发布在评论中#这是代码IOO
标签: r time-series xts