将 XTS 对象转换为 data.frame [英] Converting an XTS object to a data.frame
问题描述
可能的重复:
如何创建行名变量?
<小时>
请在 R 中运行:
require(quantmod)setSymbolLookup(SDB=list(name="000001.sz",src="yahoo"))getSymbols("SDB",from="2010-01-01",to="2010-02-01")sdb=as.data.frame(weeklyReturn(SDB))数据表
我得到的是:
weekly.returns2010-01-08 -0.078303432010-01-15 -0.051769912010-01-22 0.076994872010-01-29 -0.059792032010-02-01 -0.02119816
我想得到的是:
日期每周.返回1 2010-01-08 -0.078303432 2010-01-15 -0.051769913 2010-01-22 0.076994874 2010-01-29 -0.059792035 2010-02-01 -0.02119816
我该怎么做?
请注意,这是一个 XTS
对象,而不是基本的 data.frame
.转换后,我希望原始行名在结果 data.frame
中显示为新变量.
好的.因此,它与您之前的问题并不完全相同,因为这是一个 XTS
对象.尽管如此,还是很容易打理:
data.frame(date = index(weeklyReturn(SDB)),每周返回(SDB),row.names=NULL)# 每周日期.returns# 1 2010-01-04 -0.03303426# 2 2010-01-11 -0.04681569# 3 2010-01-18 -0.05000000# 4 2010-01-25 0.03353517# 5 2010-02-01 -0.04281208
有关此处操作的帮助,请查看使用 ?indexClass
以阅读 XTS
包中 index
的文档.>
Possible Duplicate:
How to create a variable of rownames?
Please run it in R:
require(quantmod)
setSymbolLookup(SDB=list(name="000001.sz",src="yahoo"))
getSymbols("SDB",from="2010-01-01",to="2010-02-01")
sdb=as.data.frame(weeklyReturn(SDB))
sdb
What I get is:
weekly.returns
2010-01-08 -0.07830343
2010-01-15 -0.05176991
2010-01-22 0.07699487
2010-01-29 -0.05979203
2010-02-01 -0.02119816
What I want to get is:
date weekly.returns
1 2010-01-08 -0.07830343
2 2010-01-15 -0.05176991
3 2010-01-22 0.07699487
4 2010-01-29 -0.05979203
5 2010-02-01 -0.02119816
How can I do this?
Note that this is an XTS
object, not a basic data.frame
. After the conversion, I want the original rownames to appear as a new variable in the resulting data.frame
.
Ok. So, it's not exactly the same as your earlier question since this is an XTS
object. Still, very easy to take care of:
data.frame(date = index(weeklyReturn(SDB)),
weeklyReturn(SDB), row.names=NULL)
# date weekly.returns
# 1 2010-01-04 -0.03303426
# 2 2010-01-11 -0.04681569
# 3 2010-01-18 -0.05000000
# 4 2010-01-25 0.03353517
# 5 2010-02-01 -0.04281208
For help on what is being done here, view use ?indexClass
to read the documentation for index
in the XTS
package.
这篇关于将 XTS 对象转换为 data.frame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!