将数据框转换为 xts

发布于 2024-10-05 05:07:21 字数 602 浏览 7 评论 0原文

我正在尝试使用 as.xts() 方法将数据帧转换为 xts 对象。这是我的输入数据框 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

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

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

The result is:

> 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!

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

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

发布评论

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

评论(11

时间你老了 2024-10-12 05:07:22

您可以简单地执行以下

qxts <- xts(q[,2],q$t)

对我有用的操作。

You can simply do the following

qxts <- xts(q[,2],q$t)

Worked for me.

超可爱的懒熊 2024-10-12 05:07:22

我也遇到了这个问题,但我的数据日期格式略有不同:yyyy-mm-dd,而不是 OP,这是下载到 R 的财务数据的典型格式。
具体来说,例如:“2022-02-28”。
因此,所有建议的解决方案都不起作用。
有效的方法是:

as.xts(q, order.by=as.Date(rownames(q), format = "%Y%m%d"))

假设您的数据位于典型的数据框中以日期作为行名(如果没有,只需将 q 适当替换为数据和日期列)

I ran into this as well, but my data date format was slightly different: yyyy-mm-dd as opposed to the OP, which is typical for financial data you download into R.
specifically, as an example: "2022-02-28".
As a result all the suggested solutions do not work.
What works is:

as.xts(q, order.by=as.Date(rownames(q), format = "%Y%m%d"))

assuming your data is in a typical dataframe with dates as rownames (if not, just replace q appropriately with data and date column)

清晰传感 2024-10-12 05:07:22

对于 tibbledata.frame

如 @psychonomics 注释中所示,可以使用 tk_xts

library(timetk)

qxts <- tk_xts(q)

对于 data.table

对于 data.table dtas.xts(dt)tk_xts(dt) 快得多。

因此,这里的一个简单解决方案是首先将 data.frame 转换为 data.table

library(data.table)

qxts <- as.xts(as.data.table(q))

For tibble or data.frame:

As in @psychonomics comment, one can use tk_xts:

library(timetk)

qxts <- tk_xts(q)

For data.table:

as.xts(dt) is much faster than tk_xts(dt) for a data.table dt.

So a simple solution here is to first convert the data.frame to a data.table:

library(data.table)

qxts <- as.xts(as.data.table(q))
岁月苍老的讽刺 2024-10-12 05:07:22

使用 read.zoo,然后使用 as.xts。这:

  • 除了已经使用的包之外不需要任何额外的包
  • 的内部 -

采用整个对象方法避免处理 q代码

library(xts) # also pulls in zoo
as.xts(read.zoo(q))

Use read.zoo followed by as.xts. This:

  • does not need any additional packages other than those already being used
  • employs the whole object approach avoiding dealing with the internals of q

Code--

library(xts) # also pulls in zoo
as.xts(read.zoo(q))
梦过后 2024-10-12 05:07:21

这是明确记录的 --- xtszoo 对象是通过提供两个参数、向量来形成的Matrix 携带数据和 DatePOSIXctchron、... 提供时间信息的类型(或者在 < a href="http://cran.r-project.org/package=zoo" rel="noreferrer">zoo 排序)。

所以,做类似的事情

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

,你就应该准备好了。

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).

So do something like

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

and you should be set.

〃安静 2024-10-12 05:07:21

好吧,as.xts 默认情况下假设日期存储在 data.frame 的行名中。因此出现错误消息。一个快速而肮脏的解决方法是:

rownames(q) = q[1]
as.xts(q)

但是您会得到一个带有日期字符串的额外列。理想情况下,您将构建 data.frame,并以日期作为行名开始。

Well, as.xts assumes by default that the dates are stored in the rownames of the data.frame. Hence the error message. A quick and dirty fix is:

rownames(q) = q[1]
as.xts(q)

But you get an extra column with the dates string. Ideally you would construct the data.frame with the dates as rownames to start with.

叫思念不要吵 2024-10-12 05:07:21

下面是使用 tidyquant 包的解决方案,其中包含一个将数据帧强制转换为 xts 对象的函数 as_xts()。它还包含 as_tibble() 来将 xts 对象强制转换为 tibbles(“整洁”数据帧)。

重新创建数据框(请注意,日期时间类用于“整洁”的数据框,但可以使用任何明确的日期或日期时间类):

> q
# A tibble: 3 × 2
                    t     x
               <dttm> <dbl>
1 2006-01-01 00:00:00     1
2 2006-01-01 01:00:00     2
3 2006-01-01 02:00:00     3

使用 as_xts() 转换为“xts” “ 班级。指定参数 date_col = t,以将“t”列指定为用作行名称的日期:

> library(tidyquant)
> as_xts(q, date_col = t)
                    x
2006-01-01 00:00:00 1
2006-01-01 01:00:00 2
2006-01-01 02:00:00 3

返回的是具有正确日期或日期的 xts 对象- 次作为行名称。

Here's a solution using the tidyquant package, which contains a function as_xts() that coerces a data frame to an xts object. It also contains as_tibble() to coerce xts objects to tibbles ("tidy" data frames).

Recreate the data frame (note that the date-time class is used in "tidy" data frames, but any unambiguous date or date time class can be used):

> q
# A tibble: 3 × 2
                    t     x
               <dttm> <dbl>
1 2006-01-01 00:00:00     1
2 2006-01-01 01:00:00     2
3 2006-01-01 02:00:00     3

Use as_xts() to convert to "xts" class. Specify the argument, date_col = t, to designate the "t" column as the dates to use as row names:

> library(tidyquant)
> as_xts(q, date_col = t)
                    x
2006-01-01 00:00:00 1
2006-01-01 01:00:00 2
2006-01-01 02:00:00 3

The return is an xts object with the proper date or date-times as row names.

终难愈 2024-10-12 05:07:21

这是一个可能的解决方案:

library(timetk)
q <- xts::xts(q[,-1], order.by = q$t)

Here is a posible solution:

library(timetk)
q <- xts::xts(q[,-1], order.by = q$t)
ゞ记忆︶ㄣ 2024-10-12 05:07:21

我定义了一个索引,其长度等于我的小标题的行数。仅在单独定义时间顺序后(如示例所示):

ti= seq(from = ymd_hm("2000-01-01 00:00"),
to = ymd_hm("2000-01-02 01:00"), by =  "30 min", tz = "UTC")

tbl <- tibble(t =ti,
    x = 1:length(t))
)

此代码有效:

xts.tbl <- xts(tbl[,-1], order.by = ti)

但是所有数据都转换为字符。

I defined an index with the length equal to the number of rows of my tibble. Only after defining the time sequence separately as shown with the example:

ti= seq(from = ymd_hm("2000-01-01 00:00"),
to = ymd_hm("2000-01-02 01:00"), by =  "30 min", tz = "UTC")

tbl <- tibble(t =ti,
    x = 1:length(t))
)

This code worked:

xts.tbl <- xts(tbl[,-1], order.by = ti)

However all data transformed into characters.

不甘平庸 2024-10-12 05:07:21

现在它不起作用的原因似乎很清楚,xts 不接受 tibbles,即使选择了列,它们仍然存储为 Tibbles。核心数据可以转换为矩阵或向量。以下代码有效:
xls.tbl <- xls(tbl$x, order.by = tbl$t)

The reason, why it did not work now seems clear, xts does not accept tibbles and even if columns are selected they are still stored as Tibbles. Either the core data may be transformed to matrix ore a vector.The following code works:
xls.tbl <- xls(tbl$x, order.by = tbl$t)

酒解孤独 2024-10-12 05:07:21

尝试以下操作

q$t<-as.xts(q, order.by = as.Date(q$t), dateFormat="POSIXct")

Try the following

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