如何将 CSV 数据文件加载到 R 中以与 quantmod 一起使用 [英] How to load CSV data file into R for use with quantmod

查看:35
本文介绍了如何将 CSV 数据文件加载到 R 中以与 quantmod 一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 R 的新手,刚刚开始使用它.我目前正在试验 quantmod 包.

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

quantmod 包似乎完成了我想做的大部分事情,但是,我不想使用 getSymbols() 函数将数据提取到 R 中.相反,我想使用我自己的数据 - 存储为 csv 文件我的本地光盘.

The quantmod package seems to do most of what I want to do, however, I dont want to use the getSymbols() function to fetch data into R. Instead, I want to use my own data - stored as csv files on my local disc.

我希望能够从我的 CSV 文件中提取数据以用于 quantmod.我来了 这篇文章,它展示了如何读取 CSV 文件以与 quantmod 一起使用,但我不喜欢它至少有两个原因:

I want to be able to slurp the data from my CSV files for use with quantmod. I came accros this article, which shows how to do read CSV files for use with quantmod, but I don't like it for at least 2 reasons:

  1. 它会在加载到 quantmod 之前将一个新的(重新格式化的)CSV 文件写入磁盘.我更愿意使用 R 在内存中进行任何必要的修改.

  1. It writes a new (reformatted) CSV file to disc before loading into quantmod. I would much rather do any necessary munging in memory, using R.

CSV 文件具有列标题.我的数据没有列标题.相反,这些字段位于预先确定的固定列位置(与雅虎财经数据表采用的标准"格式相匹配).

The CSV file has column headers. My data dosen't have column headers. Instead the fields are at predetermined fixed column positions (matches the 'standard' format adopted by Yahoo Finance data tables).

我还没有计算出 getSymbols() 函数返回的数据类型.我希望它返回一个数据框,但是当我检查它的类时,它被识别为一个字符向量 - 我发现这非常令人惊讶(坦率地说,不相信,因为我能够从包含的数据中绘制一个条形图在变量中):

I haven't managed to work out the data type returned by the getSymbols() function. I expected it to return a data frame, yet when I checked its class, it was identified as a character vector - which I found very suprising (and frankly, don't believe, since I am able to plot a barChart from the data contained in the variable):

yhoo <- getSymbols("YHOO",src="google")
class(yhoo)
[1] "character"
> yhoo
[1] "YHOO"

如果有人能展示如何编写一个小的 R 函数(很可能是 read.csv 的包装器),它将从我的 CSV 文件中读取数据并将其作为 R 对象(数据框?)返回以供使用,我将不胜感激使用 quantmod.

I would be grateful if someone could show how to write a small R function (most likely a wrapper around read.csv) that will read data from my CSV file and return it as an R object (data frame?) for use with quantmod.

以下是一些伪代码,解释了我想要做什么:

Here is some pseudocode explaining what I want to do:

# in case I need some funcs here for creating data type returned by function
library(quantmod) 

loadCSVDataFile <- function(full_pathname){
    csvdata <- read.csv(full_pathname, header=FALSE,sep=",")
    dates <- csvdata[,1]
    op <- csvdata[,2]
    hi <- csvdata[,3]
    lo <- csvdata[,4]
    cl <- csvdata[,5]
    vol <- csvdata[,6]
    oi <- csvdata[,7]

    # Now combine columns into a data type that matches that returned by the
    # getSymbols() ....
    # return(dataset)
}

[[更新]]

使用迄今为止给出的答案,我没有设法让 STLL 发挥作用......:

I have STLL not managed to get this to work, using the answers given so far ...:

> gbpusd <- as.xts(read.zoo('/path/to/gbpusd.csv', header=FALSE))
> class (gbpusd)
[1] "xts" "zoo"
> barChart(gbpusd)
Error in `[.xts`(x, xsubset) : subscript out of bounds

> gbpusd2 <- getSymbols.csv('gbpusd',,'/path/to/')
Error in missing(verbose) : 'missing' can only be used for arguments
> 
> gbpusd2 <- getSymbols.csv('gbpusd',.GlobalEnv,'/path/to/')
Error in missing(verbose) : 'missing' can only be used for arguments
> 
> 
> gbpusd2 <- getSymbols.csv('gbpusd','.GlobalEnv','/path/to/')
Error in missing(verbose) : 'missing' can only be used for arguments
> 
> gbpusd2 <- getSymbols.csv('gbpusd',env,'/path/to/')
Error in missing(verbose) : 'missing' can only be used for arguments

我做错了什么?

推荐答案

我可以让它工作,但你必须确定你的设置需要哪些参数.

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)

这篇关于如何将 CSV 数据文件加载到 R 中以与 quantmod 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆