我可以在 R 中使用 write.csv 编写 xts 对象吗 [英] can I write an xts object using write.csv in R

查看:27
本文介绍了我可以在 R 中使用 write.csv 编写 xts 对象吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 xts 对象,它的第一列是日期时间,然后是 OHLC.当我输入

I have an xts object, the first column of which is date-time, followed by OHLC. when I type

>test

它打印正确的输出如下:

it prints the correct output as follows:

2010-09-08 15:13:00   115   115  110    115
2010-09-08 15:14:00   120   125  115    125

但是,当我尝试 write.csv(test,"test.csv")它只写 OHLC - 为什么.我用什么命令来写日期时间

however, when I try write.csv(test,"test.csv") it only writes the OHLC - why. what command do I use to also write the date-time

这就是 str(test) 的样子:

this is what str(test) looks like:

An ‘xts’ object from 2010-06-30 15:47:00 to 2010-09-08 15:14:00 containing:
  Data: num [1:21757, 1:4] 215 220 205 195 185 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:4] "y.Open" "y.High" "y.Low" "y.Close"
  Indexed by objects of class: [POSIXlt,POSIXt] TZ: 
  xts Attributes:  
 NULL

推荐答案

是的,你可以,最简单的方法可能是通过 write.zoo:

Yes you can, and the easiest way may be via write.zoo:

R> write.zoo
function (x, file = "", index.name = "Index", row.names = FALSE, 
    col.names = NULL, ...) 
{
    if (is.null(col.names)) 
        col.names <- !is.null(colnames(x))
    dx <- as.data.frame(x)
    stopifnot(all(names(dx) != index.name))
    dx[[index.name]] <- index(x)
    dx <- dx[, c(ncol(dx), 1:(ncol(dx) - 1))]
    write.table(dx, file = file, row.names = row.names, col.names = col.names, 
        ...)
}
<environment: namespace:zoo>
R> 

这是一个完整的例子:

R> mat <- matrix(rnorm(20),5,4, dimnames=list(NULL, LETTERS[1:4]))
R> mat
              A          B         C         D
[1,] -2.5304768  0.5454043  0.754670  0.330617
[2,] -0.5199045  0.3943289 -1.271524 -2.243113
[3,] -0.0996277 -0.0513063 -0.846310 -0.140727
[4,]  0.3819981  0.5230709  1.131108  2.398311
[5,]  1.4366976 -1.7750772  0.193936  1.047754
R> xmat <- xts(mat, order.by=Sys.Date() + seq(-4,0))
R> xmat
                    A          B         C         D
2012-01-19 -2.5304768  0.5454043  0.754670  0.330617
2012-01-20 -0.5199045  0.3943289 -1.271524 -2.243113
2012-01-21 -0.0996277 -0.0513063 -0.846310 -0.140727
2012-01-22  0.3819981  0.5230709  1.131108  2.398311
2012-01-23  1.4366976 -1.7750772  0.193936  1.047754

所以现在我们有了数据,只需编写它:

So now that we have our data, it is just a matter of writing it:

R> write.zoo(xmat, file="/tmp/demo.csv", sep=",")
R> system("cat /tmp/demo.csv")
"Index","A","B","C","D"
2012-01-19,-2.53047680387774,0.545404313269755,0.754669841541681,0.330616876246245
2012-01-20,-0.519904544868541,0.394328857686792,-1.27152367237311,-2.24311276135881
2012-01-21,-0.0996276931028331,-0.0513062656752562,-0.846309564748021,-0.14072731914499
2012-01-22,0.381998053276389,0.523070920853495,1.13110826400249,2.39831100812159
2012-01-23,1.43669757366164,-1.77507724264279,0.193935657150967,1.04775355172344
R> 

Edit on 25 Jan 2012 使用 row.names=FALSE,而不是 TRUE 来抑制双行名称.由于 row.names=FALSE 是默认值,因此将其从调用中删除.

Edit on 25 Jan 2012 Use row.names=FALSE, not TRUE to suppress double row names. And as row.names=FALSE is the default, remove it from the call.

这篇关于我可以在 R 中使用 write.csv 编写 xts 对象吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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