【问题标题】:Error in converting daily stock series into monthly ones将每日股票系列转换为每月系列时出错
【发布时间】: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.periodto.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


【解决方案1】:

回答您的实际问题:问题是您在 xts 对象的 coredata 中包含 "Date" 列。 xts 和 zoo 对象是具有索引属性的简单矩阵(不是索引)。由于您不能在矩阵中混合列类型,因此您的所有数据都将转换为单一类型(在本例中为字符)。

library(xts)
# Example data
IOO <- read.csv(text="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")
# Omit 'Close' and 'Volume' columns
IOO <- IOO[, c(1,2,3,4,6)]
# Rename 'Adjusted' column to 'Close'
colnames(IOO) <- c("Date", "Open", "High", "Low", "Close")
# Convert 'Date' column to actual Date class
IOO[, "Date"] <- as.Date(IOO[, "Date"], format = "%m/%d/%Y")
# Create xts object, using 'Date' column as index, and everything else as coredata
IOO <- xts(IOO[,-1], IOO[,1])
# Aggregate to monthly
IOO_monthly <- to.monthly(IOO)
IOO_monthly
#          IOO.Open IOO.High IOO.Low IOO.Close
# Dec 2012    63.00    63.95   63.00     56.11
# Jan 2013    64.94    65.19   64.37     56.66

【讨论】:

  • 非常感谢约书亚。很有帮助!
猜你喜欢
  • 2018-03-23
  • 1970-01-01
  • 2021-02-13
  • 2018-06-23
  • 2015-03-23
  • 1970-01-01
  • 2014-07-16
  • 2021-10-08
  • 1970-01-01
相关资源
最近更新 更多