Zoo tube 8:用索引将 xts/zoo对象写入csv

> library(PerformanceAnalytics)
> data(managers)
> cl(managers)
[1] "xts" "zoo"
> head(managers)
              HAM1 HAM2    HAM3    HAM4 HAM5 HAM6 EDHEC LS EQ SP500 TR US 10Y TR US 3m TR
1996-01-31  0.0074   NA  0.0349  0.0222   NA   NA          NA   0.0340   0.00380  0.00456
1996-02-29  0.0193   NA  0.0351  0.0195   NA   NA          NA   0.0093  -0.03532  0.00398
1996-03-31  0.0155   NA  0.0258 -0.0098   NA   NA          NA   0.0096  -0.01057  0.00371
1996-04-30 -0.0091   NA  0.0449  0.0236   NA   NA          NA   0.0147  -0.01739  0.00428
1996-05-31  0.0076   NA  0.0353  0.0028   NA   NA          NA   0.0258  -0.00543  0.00443
1996-06-30 -0.0039   NA -0.0303 -0.0019   NA   NA          NA   0.0038   0.01507  0.00412

当我尝试使用write.csv将此对象导出到 csv 时,日期索引不会导出。

"","HAM1","HAM2","HAM3","HAM4","HAM5","HAM6","EDHEC LS EQ","SP500 TR","US 10Y TR","US 3m TR"
"1",0.0074,NA,0.0349,0.0222,NA,NA,NA,0.034,0.0038,0.00456
"2",0.0193,NA,0.0351,0.0195,NA,NA,NA,0.0093,-0.03532,0.00398
"3",0.0155,NA,0.0258,-0.0098,NA,NA,NA,0.0096,-0.01057,0.00371
"4",-0.0091,NA,0.0449,0.0236,NA,NA,NA,0.0147,-0.01739,0.00428
"5",0.0076,NA,0.0353,0.0028,NA,NA,NA,0.0258,-0.00543,0.00443
"6",-0.0039,NA,-0.0303,-0.0019,NA,NA,NA,0.0038,0.01507,0.00412
"7",-0.0231,NA,-0.0337,-0.0446,NA,NA,NA,-0.0442,-0.001,0.00454
"8",0.0395,-1e-04,0.0461,0.0351,NA,NA,NA,0.0211,-0.00448,0.00451
"9",0.0147,0.1002,0.0653,0.0757,NA,NA,NA,0.0563,0.02229,0.0047
"10",0.0288,0.0338,0.0395,-0.018,NA,NA,NA,0.0276,0.02869,0.00428
"11",0.0156,0.0737,0.0666,0.0458,NA,NA,NA,0.0756,0.02797,0.00427
"12",0.0176,0.0298,0.0214,0.0439,NA,NA,NA,-0.0198,-0.02094,0.00442
8

日期确实显示出来了。这是一个可重复的例子:

dfA = read.table(textConnection('row.name HAM1    HAM2    HAM3    HAM4    HAM5    HAM6 "EDHEC LS EQ"  SP500 "TR US 10Y" "TR US 3m TR"
1996-01-31  0.0074      NA  0.0349  0.0222      NA      NA          NA  0.034000   0.00380  0.00456
1996-02-29  0.0193      NA  0.0351  0.0195      NA      NA          NA  0.009300  -0.03532  0.00398
1996-03-31  0.0155      NA  0.0258 -0.0098      NA      NA          NA  0.009600  -0.01057  0.00371
1996-04-30 -0.0091      NA  0.0449  0.0236      NA      NA          NA  0.014700  -0.01739  0.00428
1996-05-31  0.0076      NA  0.0353  0.0028      NA      NA          NA  0.025800  -0.00543  0.00443
1996-06-30 -0.0039      NA -0.0303 -0.0019      NA      NA          NA  0.003800   0.01507  0.00412
1996-07-31 -0.0231      NA -0.0337 -0.0446      NA      NA          NA -0.044200  -0.00100  0.00454
1996-08-31  0.0395 -0.0001  0.0461  0.0351      NA      NA          NA  0.021100  -0.00448  0.00451
1996-09-30  0.0147  0.1002  0.0653  0.0757      NA      NA          NA  0.056300   0.02229  0.00470
1996-10-31  0.0288  0.0338  0.0395 -0.0180      NA      NA          NA  0.027600   0.02869  0.00428'), header = TRUE)
row.names(dfA) = as.Date(dfA$row.name, format = '%Y-%m-%d')
dfA$row.name = NULL
write.csv(dfA, file = 'delete.txt', row.names = TRUE)

使现代化

zoo将导致类似的处理:

library(zoo)
zooA = as.zoo(dfA, order.by = row.names(dfA))
write.csv(zooA, file = 'delete.txt', row.names = TRUE)

“”,“HAM1”,“HAM2”,“HAM3”,“HAM4”,0.00350.000.003195,“0.05040.000.00393”,0.05040.000.00393-1996 年不适用,0.05040.00395 不适用,“0.05040.000.00393-1996 年不适用”,“0.00350.000.00395 不适用”,1996 年不适用。

更新2

结果是 OP 有一个xts对象,它有一个index属性,而不是rownames属性,可以使用调用write.zoo而不是write.csv(查找rownames)。

4

正如 @ tchakravarty 指出的那样,应该使用write.zoo。这里是最适合我的:

write.zoo(tdata, filename, quote = FALSE, sep = ",")

此外,如果时间戳具有亚秒精度,则需要options(digits.secs = 6)这样的小数位数显示在 csv 文件中。

2
write.csv(t, "t.csv", row.names=TRUE)

row.names:表示“x”的行名称是否与“x”一起写入的逻辑值,或者要写入的行名称的字符向量。

2

在使用write.csv写入 csv 文件之前,可以将xts对象转换为数据帧:

write.csv(as.data.frame(managers), "filename.csv", row.names = TRUE)

本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处

(336)
免费windows服务器:如何在Windows上安装免费的本地邮件服务器
上一篇
Char At:Char*指针和char[]
下一篇

相关推荐

  • cvt变速箱结构图解:CVT变速箱的工作原理

    CVT变速箱是一种无级变速箱,它的结构由两个部分组成:输入轴和输出轴。输入轴由发动机驱动,输出轴将变速箱的输出转移到车轮上。输入轴上安装有一个可变的滑轮,它可以改变输入轴的传动比,从而改变变速箱的输出比。另外,输入轴上还安装有一个液压系统,它可以控制滑轮的位置,从而改变输入轴的传动比。…

    2023-04-02 04:00:04
    0 37 50
  • xcode代码格式化快捷键:使用Command + I快速格式化Xcode代码

    格式化的方法格式化的方法Xcode代码格式化快捷键:Option+Command+F…

    2023-07-13 12:44:06
    0 31 40
  • cv树洞柒夜事件CV树洞中的秘密与惊奇

    cv树洞柒夜事件是一个有趣的计算机视觉活动,它提供了一种新的方式来利用计算机视觉技术来探索和发现景观。它始于2020年7月,由一群热爱计算机视觉的研究者和开发者发起,他们希望通过使用机器学习技术来探索景观中的精彩之处。cv树洞柒夜事件的目标是使用机器学习技术来探索景观中的精彩之处,并利用这些发现来改善景观设计。参与者需要使用机器学习技术来探索景观,并利用这些发现来改善景观设计。参与者可以使用Python,OpenCV,TensorFlow等技术来完成任务,并将其发布在GitHub上。…

    2023-01-09 12:42:42
    0 71 91
  • win10怎么cmd打开设置:如何在Windows 10中使用CMD打开设置

    打开“开始”菜单,点击搜索框,输入cmd,然后按下回车键。在弹出的命令提示符窗口中,输入以下代码:start ms-:…

    2023-06-07 01:59:36
    0 86 80
  • win7安装cad2020:如何在Windows 7上安装AutoCAD 2020

    从官网下载CAD 2020的安装包,并双击安装文件;按照安装向导的提示,选择安装语言、安装位置和安装类型;…

    2023-09-28 00:41:59
    0 24 41
  • java protected关键字:使用protected关键字保护类成员的优点

    示例示例关键字是java中的修饰符,它可以修饰类、变量和方法。修饰类:修饰的类只能在同一个包内被访问,如果子类继承了修饰的父类,则子类可以在不同包内访问父类的成员。…

    2023-04-18 04:45:37
    0 25 83
  • codeblocks手机版下载一款强大的跨平台编程工具

    CodeBlocks是一款免费的、开源的、跨平台的C/C++ IDE,支持多种编程语言,可以用来编写、调试和编译代码。目前CodeBlocks的手机版本只支持Android系统,而且只支持C语言,不支持C++。…

    2023-06-25 04:48:50
    0 83 66
  • go和come的区别:去吧!来吧!改变你的未来

    示例示例go和come的区别:Go是一个动词,表示“去”的意思,指的是从一个地方到另一个地方的运动。Come是一个动词,表示“来”的意思,指的是从另一个地方到当前地方的运动。…

    2023-05-25 16:09:00
    0 79 66

发表评论

登录 后才能评论

评论列表(66条)