如何使用R中的write.table函数?

发布于 2024-12-22 01:59:58 字数 1111 浏览 2 评论 0原文

我正在尝试使用命令 write.table(ESH2, "c:/ESH2.txt", sep=",") 将对象保存到文本文件。问题是保存的时间序列不包含我下载的日期和时间值。我使用了包 twsInstrument 和命令 getBAT(ESH2)

使用命令 load(file = "C:/ESH2) 将其加载到 R 时获得的数据.Rdata")

               ES.Bid.Price ES.Ask.Price ES.Trade.Price ES.Mid.Price ES.Volume
1323700200        1237.25        1237.50        1237.50     1237.375      6954
1324057980        1210.25        1210.50        1210.25     1210.375      3792
1324058040        1210.50        1211.00        1211.00     1210.750      3305
.........
.........
.........
1324058100           NA           NA             NA           NA         823

attr(,".indexCLASS")
[1] POSIXct POSIXt 
attr(,".indexTZ")
[1] 
attr(,"from")
[1] 20111211  23:59:59
attr(,"to")
[1] 20111216  23:59:59
attr(,"src")
[1] IB
attr(,"updated")
[1] "2011-12-16 18:54:55 CET"

第一列应显示 Date_Time 而不是 1323700200。

我正在寻找一种每周下载一次数据并合并数据的简单方法。

ps 是的,我可以阅读教程/书籍来完成此任务,是的,我会这样做,但问题是我缺乏时间。我想在本周开始收集数据,因为交互式代理限制数据请求1 分钟数据 = 最多 5 天。我感谢任何帮助和建议。

I am trying to save an object to a text file with the command write.table(ESH2, "c:/ESH2.txt", sep=","). The problem is that the saved time series doesn't contain date and time values that I downloaded. I used the package twsInstrument and the command getBAT(ESH2)

The data i have when loading it into R with the command load(file = "C:/ESH2.Rdata")

               ES.Bid.Price ES.Ask.Price ES.Trade.Price ES.Mid.Price ES.Volume
1323700200        1237.25        1237.50        1237.50     1237.375      6954
1324057980        1210.25        1210.50        1210.25     1210.375      3792
1324058040        1210.50        1211.00        1211.00     1210.750      3305
.........
.........
.........
1324058100           NA           NA             NA           NA         823

attr(,".indexCLASS")
[1] POSIXct POSIXt 
attr(,".indexTZ")
[1] 
attr(,"from")
[1] 20111211  23:59:59
attr(,"to")
[1] 20111216  23:59:59
attr(,"src")
[1] IB
attr(,"updated")
[1] "2011-12-16 18:54:55 CET"

The first column should show Date_Time and not 1323700200.

I am searching for an easy way to download the data once per week and merge the data.

p.s Yes i can read through the tutorials/books to accomplish this and yes i will do it but the problem is my lack of time. I want to start collecting the data this week cause interactive brokers is limiting data requests 1min data = 5DAYS maximum. I am thankful for any help and suggestions.

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

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

发布评论

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

评论(2

踏雪无痕 2024-12-29 01:59:58

您可能有一个 xts 或 Zoo 对象,并且需要使用 write.zoo 函数。如果我对“ESH2”对象的结构的理解是正确的,那么您称为“第一列”的数据实际上是行名,用zoo/xts的说法是“索引”,而可以使用 < 访问数据code>coredata 是一个矩阵对象。

从 read/write.zoo 页面上的示例中:

Lines <- "CVX 20070201 9 30 51 73.25 81400 0
CVX 20070201 9 30 51 73.25 100 0
CVX 20070201 9 30 51 73.25 100 0
CVX 20070201 9 30 51 73.25 300 0
CVX 20070201 9 30 51 73.25 81400 0
CVX 20070201 9 40 51 73.25 100 0
CVX 20070201 9 40 52 73.25 100 0
CVX 20070201 9 40 53 73.25 300 0"

z <- read.zoo(textConnection(Lines), 
    colClasses = c("NULL", "NULL", "numeric", "numeric", "numeric", "numeric",
        "numeric", "NULL"), 
    col.names = c("Symbol", "Date", "Hour", "Minute", "Second", "Price", 
        "Volume", "junk"),
    index = 1:3,  # do not count columns that are "NULL" in colClasses
    FUN = function(h, m, s) times(paste(h, m, s, sep = ":")),
    FUN2 = function(tt) trunc(tt, "00:00:05"),
    aggregate = mean)
# The only material I added.
write.zoo(z)
"Index" "Price" "Volume"
09:30:50 73.25 32660
09:40:50 73.25 166.666666666667

You probably have an xts or zoo object and need to use the write.zoo function. If I'm right about the structure of the "ESH2" object, then the data that you are calling the "first column" is actually the rownames which in zoo/xts parlance is the "index" while the data can be accessed with coredata and is a matrix object.

From the examples on the read/write.zoo page:

Lines <- "CVX 20070201 9 30 51 73.25 81400 0
CVX 20070201 9 30 51 73.25 100 0
CVX 20070201 9 30 51 73.25 100 0
CVX 20070201 9 30 51 73.25 300 0
CVX 20070201 9 30 51 73.25 81400 0
CVX 20070201 9 40 51 73.25 100 0
CVX 20070201 9 40 52 73.25 100 0
CVX 20070201 9 40 53 73.25 300 0"

z <- read.zoo(textConnection(Lines), 
    colClasses = c("NULL", "NULL", "numeric", "numeric", "numeric", "numeric",
        "numeric", "NULL"), 
    col.names = c("Symbol", "Date", "Hour", "Minute", "Second", "Price", 
        "Volume", "junk"),
    index = 1:3,  # do not count columns that are "NULL" in colClasses
    FUN = function(h, m, s) times(paste(h, m, s, sep = ":")),
    FUN2 = function(tt) trunc(tt, "00:00:05"),
    aggregate = mean)
# The only material I added.
write.zoo(z)
"Index" "Price" "Volume"
09:30:50 73.25 32660
09:40:50 73.25 166.666666666667
等待我真够勒 2024-12-29 01:59:58

如果您的唯一目标是保存该对象,以便将来可以再次从 R 访问它,那么 using

save(ESH2, file = "C:/ESH2.Rdata") # or whatever you want to call the saved file

将为您做到这一点。您可以稍后使用 load: 再次获取对象:

load(file = "C:/ESH2.Rdata")

但正如评论中提到的,如果您需要帮助让 write.table 或 write.csv 解决方案正常工作,那么您需要提供更多信息。

If your only goal is to save the object so that you can access it from R again in the future then using

save(ESH2, file = "C:/ESH2.Rdata") # or whatever you want to call the saved file

will do that for you. You can get the object back again later by using load:

load(file = "C:/ESH2.Rdata")

But as mentioned in the comments if you want help getting the write.table or write.csv solution to work then you'll need to provide more information.

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