将 data.frame 转换为 xts 对象并保留类型
有没有办法从 data.frame 创建 xts 对象并保留数据类型?我的数字正在转换为字符。 2009 年的这篇文章建议将列合并到现有的 xts 中: http://r.789695.n4.nabble.com/as-xts-convert-all-my-numeric-data-to-character-td975564.html
不清楚这是否是唯一的方法。对于大型数据帧来说似乎有点麻烦和麻烦。我认为开箱即用的 xts 会尊重数据类型。
Is there a way to create an xts object from a data.frame and preserve data type? My numerics are being converted to character. This post from 2009 suggests merging columns into an existing xts:
http://r.789695.n4.nabble.com/as-xts-convert-all-my-numeric-data-to-character-td975564.html
It wasn't clear whether that is the ONLY way to do this. Seems a bit of a hack and cumbersome for large data frames. I would think out-of-the-box xts would respect the datatypes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,你不能。 xts/zoo 对象是一个具有索引属性的矩阵,您不能在矩阵中混合类型。
我们考虑过创建一个 xts-data.frame 类,但 xts 的主要关注点是速度和内存效率。 data.frames 的速度和内存效率不高,因此这不是一个优先事项。
No, you can't. xts/zoo objects are a matrix with an index attribute and you can't mix types in a matrix.
We've considered creating an xts-data.frame class but a primary concern of xts is speed and memory efficiency. data.frames are not speed and memory efficient, so this hasn't been a priority.
我遇到了同样的问题,我的解决方案是在指定数据对象时不包含时间列。只要所有其他列都是同一类型,就应该没有问题。
即
data <- xts(data[,2:6], order.by = data$time, unique = FALSE, tzone = "")
(data$time 是第一列,并且是 POSIXct,所以我将其排除在外。其他一切都是数字)
I had the same problem, my solution was to not include the time column when specifying the data object. As long as all the other columns are of the same type, there should be no problems.
i.e.
data <- xts(data[,2:6], order.by = data$time, unique = FALSE, tzone = "")
(data$time is the first column and is POSIXct so I'm excluding it. everything else is numeric)