将数据框转换为 xts [英] Converting a data frame to xts

查看:22
本文介绍了将数据框转换为 xts的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 as.xts() 方法将数据框转换为 xts 对象.这是我的输入数据框 q:

I'm trying to convert a data frame to xts object using the as.xts()-method. Here is my input dataframe q:

q
                      t x  
1  2006-01-01 00:00:00  1  
2  2006-01-01 01:00:00  2  
3  2006-01-01 02:00:00  3

str(q)
    'data.frame':   10 obs. of  2 variables:
 $ t: POSIXct, format: "2006-01-01 00:00:00" "2006-01-01 01:00:00" "2006-01-01 02:00:00" "2006-01-01 03:00:00" ...  
 $ x: int  1 2 3 4 5 6 7 8 9 10

结果是:

> as.xts(q)
Error in as.POSIXlt.character(x, tz, ...) : 
  character string is not in a standard unambiguous format

这是我能想到的最简单的例子,所以不能让它工作真是令人沮丧......感谢任何帮助!

This is the simplest example I can think of, so it's quite frustrating not getting it to work... Any help is appreciated!

推荐答案

这有明确的记录 --- xtszoo 对象是通过提供两个来形成的em> 参数,一个 vectormatrix 携带数据和 DatePOSIXctchron, ... 类型提供时间信息(或在 zoo 的情况下排序).

This is clearly documented --- xts and zoo objects are formed by supplying two arguments, a vector or matrix carrying data and Date, POSIXct, chron, ... type supplying the time information (or in the case of zoo the ordering).

所以做一些类似的事情

 qxts <- xts(q[,-1], order.by=q[,1])

你应该准备好了.

这篇关于将数据框转换为 xts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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