将数据框转换为 xts
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
您可以简单地执行以下
对我有用的操作。
You can simply do the following
Worked for me.
我也遇到了这个问题,但我的数据日期格式略有不同: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)
对于
tibble
或data.frame
:如 @psychonomics 注释中所示,可以使用
tk_xts
:对于
data.table
:对于
data.table
dt
,as.xts(dt)
比tk_xts(dt)
快得多。因此,这里的一个简单解决方案是首先将
data.frame
转换为data.table
:For
tibble
ordata.frame
:As in @psychonomics comment, one can use
tk_xts
:For
data.table
:as.xts(dt)
is much faster thantk_xts(dt)
for adata.table
dt
.So a simple solution here is to first convert the
data.frame
to adata.table
:使用 read.zoo,然后使用 as.xts。这:
采用整个对象方法避免处理 q代码
Use read.zoo followed by as.xts. This:
Code--
这是明确记录的 --- xts 和 zoo 对象是通过提供两个参数、
向量
或来形成的Matrix
携带数据和Date
、POSIXct
、chron
、... 提供时间信息的类型(或者在 < a href="http://cran.r-project.org/package=zoo" rel="noreferrer">zoo 排序)。所以,做类似的事情
,你就应该准备好了。
This is clearly documented --- xts and zoo objects are formed by supplying two arguments, a
vector
ormatrix
carrying data andDate
,POSIXct
,chron
, ... type supplying the time information (or in the case of zoo the ordering).So do something like
and you should be set.
好吧,as.xts 默认情况下假设日期存储在 data.frame 的行名中。因此出现错误消息。一个快速而肮脏的解决方法是:
但是您会得到一个带有日期字符串的额外列。理想情况下,您将构建 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:
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.
下面是使用
tidyquant
包的解决方案,其中包含一个将数据帧强制转换为 xts 对象的函数as_xts()
。它还包含as_tibble()
来将 xts 对象强制转换为 tibbles(“整洁”数据帧)。重新创建数据框(请注意,日期时间类用于“整洁”的数据框,但可以使用任何明确的日期或日期时间类):
使用
as_xts()
转换为“xts” “ 班级。指定参数date_col = t
,以将“t”列指定为用作行名称的日期:返回的是具有正确日期或日期的
xts
对象- 次作为行名称。Here's a solution using the
tidyquant
package, which contains a functionas_xts()
that coerces a data frame to an xts object. It also containsas_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):
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:The return is an
xts
object with the proper date or date-times as row names.这是一个可能的解决方案:
Here is a posible solution:
我定义了一个索引,其长度等于我的小标题的行数。仅在单独定义时间顺序后(如示例所示):
此代码有效:
但是所有数据都转换为字符。
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:
This code worked:
However all data transformed into characters.
现在它不起作用的原因似乎很清楚,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)
尝试以下操作
Try the following