Convert data.frame to xts object and preserve types

后端 未结 2 1770
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-04 19:42

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 int

相关标签:
2条回答
  • 2021-02-04 20:27

    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.

    0 讨论(0)
  • 2021-02-04 20:29

    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)

    0 讨论(0)
提交回复
热议问题