R:将数据帧(混合因子和数字)转换为 R 中的 XTS
将具有混合因子和数字列的数据框转换为 xts 时,我的所有数据都会转换为字符串。这对于因素来说不是问题,但是对于数字来说却非常烦人。有解决方法吗?
例如:
> x
marketTimestamp price id
1 2010-12-17 11:38:31.100 83.89 b-0
2 2010-12-17 11:38:31.100 83.88 b-1
3 2010-12-17 11:38:31.100 83.87 b-2
4 2010-12-17 11:38:31.300 83.91 o-0
5 2010-12-17 11:38:31.300 83.92 o-1
6 2010-12-17 11:38:31.300 83.93 o-2
> as.xts(x[,-1],as.POSIXct(x[,1]))
price id
2010-12-17 11:38:31 "83.89" "b-0"
2010-12-17 11:38:31 "83.88" "b-1"
2010-12-17 11:38:31 "83.87" "b-2"
2010-12-17 11:38:31 "83.91" "o-0"
2010-12-17 11:38:31 "83.92" "o-1"
2010-12-17 11:38:31 "83.93" "o-2"
理想情况下,我希望第一列保留数字,而第二列转换为字符串。该解决方案需要完全自动化,因为我正在处理具有大量列的数据集,并且我无法总是预测哪些将是因子,哪些将是数字。
--
编辑:
我试图通过定义以下函数来解决这个问题:
to.xts <- function(data) {
timestamp <- as.POSIXct(data[,1])
coredata <- data[,-1]
headers <- names(coredata)
data.type <- c()
for (header in headers) {
data.type[headers==header] <- class(coredata[[header]])
}
data.factor <- xts(coredata[,data.type=="factor"],timestamp)
data.numeric <- xts(coredata[,data.type=="numeric"],timestamp)
data.xts <- cbind(data.factor,data.numeric)
}
但是当合并两个 XTS 对象时,字符串数据被转换为 NA:
> x
id side
2010-12-17 11:38:31 "b-0" "BID"
2010-12-17 11:38:31 "b-1" "BID"
2010-12-17 11:38:31 "b-2" "BID"
> y
price
2010-12-17 11:38:31 83.89
2010-12-17 11:38:31 83.88
2010-12-17 11:38:31 83.87
> merge(x,y)
id side price
2010-12-17 11:38:31 NA NA 83.89
2010-12-17 11:38:31 NA NA 83.88
2010-12-17 11:38:31 NA NA 83.87
Warning message:
In merge.xts(x, y) : NAs introduced by coercion
这是 XTS 包的已知问题,还是我正在做某事错误的?
When converting a data frame with mixed factor and numeric columns to an xts, all of my data gets converted to strings. This isn't a problem with the factors, but it's extremely annoying with the numerics. Is there a workaround?
For example:
> x
marketTimestamp price id
1 2010-12-17 11:38:31.100 83.89 b-0
2 2010-12-17 11:38:31.100 83.88 b-1
3 2010-12-17 11:38:31.100 83.87 b-2
4 2010-12-17 11:38:31.300 83.91 o-0
5 2010-12-17 11:38:31.300 83.92 o-1
6 2010-12-17 11:38:31.300 83.93 o-2
> as.xts(x[,-1],as.POSIXct(x[,1]))
price id
2010-12-17 11:38:31 "83.89" "b-0"
2010-12-17 11:38:31 "83.88" "b-1"
2010-12-17 11:38:31 "83.87" "b-2"
2010-12-17 11:38:31 "83.91" "o-0"
2010-12-17 11:38:31 "83.92" "o-1"
2010-12-17 11:38:31 "83.93" "o-2"
Ideally I want the first column to remain numeric, whilst the second is converted to a string. The solution needs to be fully automated, as I am working with data sets with a large number of columns, and I can't always predict which ones will be factor and which will be numeric.
--
Edit:
I've tried to get around this problem by defining the following function:
to.xts <- function(data) {
timestamp <- as.POSIXct(data[,1])
coredata <- data[,-1]
headers <- names(coredata)
data.type <- c()
for (header in headers) {
data.type[headers==header] <- class(coredata[[header]])
}
data.factor <- xts(coredata[,data.type=="factor"],timestamp)
data.numeric <- xts(coredata[,data.type=="numeric"],timestamp)
data.xts <- cbind(data.factor,data.numeric)
}
but when merging the two XTS objects, the string data is converted to NAs:
> x
id side
2010-12-17 11:38:31 "b-0" "BID"
2010-12-17 11:38:31 "b-1" "BID"
2010-12-17 11:38:31 "b-2" "BID"
> y
price
2010-12-17 11:38:31 83.89
2010-12-17 11:38:31 83.88
2010-12-17 11:38:31 83.87
> merge(x,y)
id side price
2010-12-17 11:38:31 NA NA 83.89
2010-12-17 11:38:31 NA NA 83.88
2010-12-17 11:38:31 NA NA 83.87
Warning message:
In merge.xts(x, y) : NAs introduced by coercion
Is this a known problem with the XTS package, or am I doing something wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能执行此操作,因为 xts 需要数字矩阵。
You cannot do this as xts requires a numeric matrix.
这是设计上的限制。请记住,xts 或zoo 基本上是一个矩阵加索引。不是数据帧加索引。
It is a limitation by design. keep in mind that xts or zoo is basically a matrix plus index. not a dataframe plus index.