How to load CSV data file into R for use with quantmod

淺唱寂寞╮ 提交于 2019-11-30 04:08:03

I can make it work, but you have to determine which parameters are needed for your setup.

library(quantmod)

# create sample data
getSymbols("SPY")
write.zoo(SPY, file="SPY.csv", sep=",")

# set symbol lookup
setSymbolLookup(SPY=list(src="csv",format="%Y-%m-%d"))
# call getSymbols(.csv) with auto.assign=FALSE
spy <- getSymbols("SPY", auto.assign=FALSE)
barChart(spy)

The common idiom is:

yhoo = as.xts(read.zoo("yhoo.csv",header=T))

If you want to use a quantmod function, then you can tell getSymbols() to use a csv file. See http://www.quantmod.com/documentation/getSymbols.csv.html I.e.

getSymbols('yhoo',src='csv')

(I've followed your lowercase convention, but remember the filenames will be case-sensitive; directory defaults to current directory and can be specified explicitly with the dir parameter, see ?getSymbols.csv)

If you include a ls() in your code, you'll find that you missed a variable:

yhoo <- getSymbols("YHOO", src = "google")
ls()
# [1] "yhoo" "YHOO"
class(yhoo)
# [1] "character"
class(YHOO)
# [1] "xts" "zoo"

So getSymbols() creates 2 variables: the character vector "yhoo" and "YHOO", an object of class extensible time series (xts) which extends the zoo class, for storing irregular time series data.

To see the documentation, use:

?xts # or ?zoo

In particular, the documentation for xts describes the as.xts() function, to convert from a matrix to xts. It should be straightforward from there to read in your own CSV files using read.csv or read.table and convert to xts objects.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!