Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Converting data frame (mixed factor and numeric) to XTS in R

Tags:

dataframe

r

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?

like image 839
Chris Taylor Avatar asked Jan 20 '23 21:01

Chris Taylor


2 Answers

You cannot do this as xts requires a numeric matrix.

like image 65
Dirk Eddelbuettel Avatar answered Jan 27 '23 02:01

Dirk Eddelbuettel


It is a limitation by design. keep in mind that xts or zoo is basically a matrix plus index. not a dataframe plus index.

like image 40
learnbasicR Avatar answered Jan 27 '23 04:01

learnbasicR