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

前端 未结 3 828
迷失自我
迷失自我 2020-12-29 01:54

I am new to R and have just started to use it. I am currently experimenting with the quantmod package.

The quantmod package seems to do most of what I want to do, ho

相关标签:
3条回答
  • 2020-12-29 02:27

    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.

    0 讨论(0)
  • 2020-12-29 02:36

    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)
    
    0 讨论(0)
  • 2020-12-29 02:38

    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)

    0 讨论(0)
提交回复
热议问题