如何将XTS更改为data.frame并将索引保留在R中? [英] How can I change XTS to data.frame and keep Index in R?
问题描述
我在R中有一个XTS timeseries格式,我试图进行一些处理,子集和重新排列,然后导出为另一个程序的工作CSV。
I have an XTS timeseries in R of the following format and am trying to do some processing, subsetting and re-arranging before exporting as a CSV for work in another program.
head(master_1)
S_1
2010-03-03 00:00:00 2.8520
2010-03-03 00:30:00 2.6945
2010-03-03 01:00:00 2.5685
2010-03-03 01:30:00 2.3800
2010-03-03 02:00:00 2.2225
2010-03-03 02:30:00 2.0650
和
str(master_1)
An ‘xts’ object from 2010-03-03 to 2010-05-25 08:30:00 containing:
Data: num [1:4000, 1] 2.85 2.69 2.57 2.38 2.22 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr "S_1"
Indexed by objects of class: [POSIXt,POSIXct] TZ:
Original class: 'zoo'
xts Attributes:
List of 1
$ dateFormat: chr "Date"
我想将其转换为data.frame所以我可以操纵它更容易,然后导出到另一个程序。但是,当我使用 test1< - as.data.frame(master_1)
时,test1确实有Index(即日期和时间)可见,
And I would like to convert this to a data.frame so I can manipulate it more easily and then export to another program. However, when I use test1 <- as.data.frame(master_1)
the test1 does have the Index (i.e. the dates and times) visible,
head(test1)
S_1
2010-03-03 00:00:00 2.8520
2010-03-03 00:30:00 2.6945
2010-03-03 01:00:00 2.5685
2010-03-03 01:30:00 2.3800
2010-03-03 02:00:00 2.2225
2010-03-03 02:30:00 2.0650
但索引没有显示,
str(test1)
'data.frame': 4000 obs. of 1 variable:
$ S_1: num 2.85 2.69 2.57 2.38 2.22 ...
写一个csv write.csv(master_1,file =master_1.csv)
不包括时间或日期。为什么会这样,我如何将数据/时间数据作为一列列出来,因此它被用于其他R命令并正确导出?
And writing a csv write.csv(master_1, file="master_1.csv")
does not include the time or date. Why is this, and how can I include the data/time data as a column, so it is used in other R commands and exported properly?
感谢任何帮助。
推荐答案
这是因为日期是data.frame中的rownames。你需要使它们成为一个单独的列。
That's because the dates are rownames in your data.frame. You need to make them a separate column.
尝试这样:
data.frame(date=index(master_1), coredata(master_1))
这篇关于如何将XTS更改为data.frame并将索引保留在R中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!