When converting a data frame with mixed factorial and numeric columns to xts, all my data is converted to rows. This is not a problem with factors, but it is an extremely annoying number. Is there a workaround?
For instance:
> 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 and the second to be converted to a string. The solution should be fully automated, since I work with data sets with a large number of columns, and I can not always predict which of them will be a coefficient and which will be numerical.
-
Edit:
I tried to work around this problem by specifying 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 combining two XTS objects, the string data is converted to 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
Is this a known issue with the XTS package, or am I something wrong?