如何将 XTS 更改为 data.frame 并保留 Index?

发布于 2024-09-12 14:39:38 字数 1290 浏览 2 评论 0原文

我在 R 中有一个以下格式的 XTS 时间序列,并尝试在导出为 CSV 以在另一个程序中工作之前进行一些处理、子集化和重新排列。

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 确实具有可见的索引(即日期和时间),

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 命令中使用并正确导出?

感谢您的任何帮助。

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

and

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"

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 

But the Index is not shown,

str(test1)
'data.frame': 4000 obs. of  1 variable:
 $ S_1: num  2.85 2.69 2.57 2.38 2.22 ...

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?

Thanks for any help.

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

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

发布评论

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

评论(6

神妖 2024-09-19 14:39:38

这是因为日期是 data.frame 中的行名。您需要将它们设为单独的列。

试试这个:

 data.frame(date=index(master_1), coredata(master_1))

That's because the dates are rownames in your data.frame. You need to make them a separate column.

Try this:

 data.frame(date=index(master_1), coredata(master_1))
冬天旳寂寞 2024-09-19 14:39:38

这有点侧边栏的意思,但是 ggplot2 包中的 fortify(...) 函数会将各种对象转换为适合在 中使用的数据框ggplot(...),包括 xts 对象。

library(xts)
set.seed(1)    # for reproducible example
master_1 <- xts(rnorm(10,mean=2,sd=0.1),as.POSIXct("2010-03-03")+30*(0:9))

library(ggplot2)
df <- fortify(master_1)
head(df)
#                  Index master_1
# 1  2010-03-03 00:00:00 1.937355
# 2  2010-03-03 00:00:30 2.018364
# 3  2010-03-03 00:01:00 1.916437
# 4  2010-03-03 00:01:30 2.159528
# 5  2010-03-03 00:02:00 2.032951
# 6  2010-03-03 00:02:30 1.917953

因此,如果您已经在使用 gggplot,这是一种简单的方法。请注意,索引进入名为 Index 的列(大写“I”)。

This is a bit of a sidebar, but the fortify(...) function in package ggplot2 will convert a variety of objects to data frames suitable for use in ggplot(...), including xts objects.

library(xts)
set.seed(1)    # for reproducible example
master_1 <- xts(rnorm(10,mean=2,sd=0.1),as.POSIXct("2010-03-03")+30*(0:9))

library(ggplot2)
df <- fortify(master_1)
head(df)
#                  Index master_1
# 1  2010-03-03 00:00:00 1.937355
# 2  2010-03-03 00:00:30 2.018364
# 3  2010-03-03 00:01:00 1.916437
# 4  2010-03-03 00:01:30 2.159528
# 5  2010-03-03 00:02:00 2.032951
# 6  2010-03-03 00:02:30 1.917953

So if you're already using gggplot this is an easy way to do it. Note that the index goes into a column named Index (capital "I").

阳光下的泡沫是彩色的 2024-09-19 14:39:38

1.9.6开始,您可以直接从/到xts转换,而不会丢失索引类。很简单:

as.data.table(master_1)

索引被添加为结果 data.table 中的第一列,它保留索引 DatePOSIXct 类。

Since 1.9.6 You can convert directly from/to xts without losing index class. As simple as:

as.data.table(master_1)

The index is added as the first column in the result data.table, it retains index Date or POSIXct classes.

北城挽邺 2024-09-19 14:39:38

您可以使用 zoo::fortify.zoo() 将 xts 对象转换为 data.frame,其中包含作为名为“Index”的列的索引。

您不需要 ggplot2,但如果您加载了 xts(或 Zoo)和 ggplot2,这仍然可以工作。

例如:

library(xts)
data(sample_matrix)
x <- as.xts(sample_matrix, dateFormat = "Date")
my_df <- fortify.zoo(x)
head(my_df)
#        Index     Open     High      Low    Close
# 1 2007-01-02 50.03978 50.11778 49.95041 50.11778
# 2 2007-01-03 50.23050 50.42188 50.23050 50.39767
# 3 2007-01-04 50.42096 50.42096 50.26414 50.33236
# 4 2007-01-05 50.37347 50.37347 50.22103 50.33459
# 5 2007-01-06 50.24433 50.24433 50.11121 50.18112
# 6 2007-01-07 50.13211 50.21561 49.99185 49.99185
str(my_df)
# 'data.frame': 180 obs. of  5 variables:
#  $ Index: Date, format: "2007-01-02" "2007-01-03" ...
#  $ Open : num  50 50.2 50.4 50.4 50.2 ...
#  $ High : num  50.1 50.4 50.4 50.4 50.2 ...
#  $ Low  : num  50 50.2 50.3 50.2 50.1 ...
#  $ Close: num  50.1 50.4 50.3 50.3 50.2 ...

You can convert an xts object to a data.frame that includes the index as a column named "Index" with zoo::fortify.zoo().

You don't need ggplot2, but this will still work if you have xts (or zoo) and ggplot2 loaded.

For example:

library(xts)
data(sample_matrix)
x <- as.xts(sample_matrix, dateFormat = "Date")
my_df <- fortify.zoo(x)
head(my_df)
#        Index     Open     High      Low    Close
# 1 2007-01-02 50.03978 50.11778 49.95041 50.11778
# 2 2007-01-03 50.23050 50.42188 50.23050 50.39767
# 3 2007-01-04 50.42096 50.42096 50.26414 50.33236
# 4 2007-01-05 50.37347 50.37347 50.22103 50.33459
# 5 2007-01-06 50.24433 50.24433 50.11121 50.18112
# 6 2007-01-07 50.13211 50.21561 49.99185 49.99185
str(my_df)
# 'data.frame': 180 obs. of  5 variables:
#  $ Index: Date, format: "2007-01-02" "2007-01-03" ...
#  $ Open : num  50 50.2 50.4 50.4 50.2 ...
#  $ High : num  50.1 50.4 50.4 50.4 50.2 ...
#  $ Low  : num  50 50.2 50.3 50.2 50.1 ...
#  $ Close: num  50.1 50.4 50.3 50.3 50.2 ...
风铃鹿 2024-09-19 14:39:38

谢恩是对的。您可能正在寻找索引(您的 xts)。这是一个可重现的示例。

library(xts)
example(xts)
x = head(sample.xts)
datefield = index(x)
newdf = data.frame(x,datefield)

然后您应该能够简单地将其导出到 csv。当然,您也可以重命名行。

Shane is right. you might be looking for index(your xts). Here's a reproducible example.

library(xts)
example(xts)
x = head(sample.xts)
datefield = index(x)
newdf = data.frame(x,datefield)

Then you should be able to simply export it to a csv. Of course you can rename the rows, too.

陈独秀 2024-09-19 14:39:38

将 XTS 更改为 data.frame 的优雅形式:

myDF <- as.data.frame(as.matrix(myXTS))

A elegant form to change XTS to data.frame:

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