如何将 CSV 数据文件加载到 R 中以与 quantmod 一起使用

发布于 2024-12-28 14:03:05 字数 2320 浏览 1 评论 0原文

我是 R 新手,刚刚开始使用它。我目前正在尝试 quantmod 包。

Quantmod 包似乎可以完成我想做的大部分事情,但是,我不想使用 getSymbols() 函数将数据获取到 R 中。相反,我想使用我自己的数据 - 存储为本地磁盘上的 csv 文件。

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

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

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

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

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

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

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

# 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 无法使其正常工作......:

> 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 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, 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.

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. 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.

  2. 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).

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"

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)
}

[[Update]]

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

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

-柠檬树下少年和吉他 2025-01-04 14:03:05

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

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)

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)
自我难过 2025-01-04 14:03:05

常见的习惯用法是:

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

如果您想使用 quantmod 函数,那么您可以告诉 getSymbols() 使用 csv 文件。请参阅http://www.quantmod.com/documentation/getSymbols.csv.html

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

(我遵循了您的小写约定,但请记住文件名区分大小写;目录默认为当前目录,可以使用 dir 参数显式指定,请参阅?getSymbols.csv)

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)

护你周全 2025-01-04 14:03:05

如果您在代码中包含 ls(),您会发现错过了一个变量:

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

因此 getSymbols() 创建了 2 个变量:字符向量“yhoo”和“YHOO”,一个可扩展时间序列类的对象 (xts),它扩展了zoo类,用于存储不规则的时间序列数据。

要查看文档,请使用:

?xts # or ?zoo

特别是,xts 的文档描述了 as.xts() 函数,用于从矩阵转换为 xts。从那里可以直接使用 read.csv 或 read.table 读取您自己的 CSV 文件并转换为 xts 对象。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文