在正确的格式quantmod难度加载数据

问题描述 投票:-1回答:1

我是很新的R,我观看YouTube视频做各种时间序列分析,但它从雅虎下载的数据 - 我的数据是在Excel中。我想按照同样的分析,但是从excel.csv文件数据。我花了两天时间找出该日期必须是在美国的风格。现在我的一个基本的一步再次卡住 - 加载数据,以便进行分析 - 这似乎是最大的障碍与R.请能有人给我为什么如下所示的命令不会完全做的回报一些指导列集。我尝试了动物园的格式,但它没有工作,那么我想XTS,它部分的工作。我怀疑从Excel原装进口是主要问题。我可以得到一些指导,请

> AllPrices <- as.zoo(AllPrices)
> head(AllPrices)
             Index1   Index2   Index3  Index4   Index5   Index6   Index7   Index8   Index9 Index10

> AllRets <- dailyReturn(AllPrices)
Error in NextMethod("[<-") : incorrect number of subscripts on matrix
> AllPrices<- as.xts(AllPrices)
> AllRets <- dailyReturn(AllPrices)
> head(AllRets)
           daily.returns
2012-11-06  0.000000e+00
2012-11-07 -2.220249e-02
2012-11-08  1.379504e-05
2012-11-09  2.781961e-04
2012-11-12 -2.411128e-03
2012-11-13  7.932869e-03
quantmod
1个回答
0
投票

尝试使用readr包加载数据。

library(readr)

然后,看文档通过在控制台中运行?read_csv

我建议这种方式读取数据。指定列类型。举例来说,如果你的第一列是日期,在读它作为一个字符“C”,如果你的另一列是数字的使用“N”。

data <- read_csv('YOUR_DATA.csv', col_types = "cnnnnn") # date in left column, 5 numeric columns
data$Dates <- as.Date(data$Dates, format = "%Y-%m-%d") # make the dates column a date class (you need to update "Dates" to be your column name for the Dates column, you may need to change the format
data <- as.data.frame(data) # turn the result into a dataframe
data <- xts(data[,-1], order.by = XAU[,1]) # then make an xts, data is everything but the date column, order.by is the date column
© www.soinside.com 2019 - 2024. All rights reserved.