强制xts()对象为ts()

时间:2015-09-08 10:33:24

标签: r dataset xts

数据:DOWNLOAD .TXT

代码:

<ion-side-menus enable-menu-with-back-views="false">
    <ion-side-menu-content>
        <ion-nav-bar class="bar-stable">
            <ion-nav-back-button>
            </ion-nav-back-button>
            <ion-nav-buttons side="left">
                <button class="button button-icon button-clear ion-navicon" menu-toggle="left">
                </button>
            </ion-nav-buttons>
        </ion-nav-bar>
        <ion-nav-view name="menuContent"></ion-nav-view>
    </ion-side-menu-content>
    <ion-side-menu side="left">
        <ion-header-bar class="bar-stable">
            <h1 class="title">Menu</h1>
        </ion-header-bar>
        <ion-content class="menubox">
            <ion-list>
                <ion-item menu-close href="#/app/home">Home</ion-item>
            </ion-list>
        </ion-content>
    </ion-side-menu>
</ion-side-menus>

我尝试做的是将xts data = read.table("DistrBdaily1yrs.txt", header = TRUE, sep = "", dec = ",") data$DATE = as.Date(as.character(data$DATE),format="%Y%m%d") dataXts = xts(data$QUANTITY,data$DATE, frequency = 6) tseries = ts(dataXts, start = start(dataXts), end = end(dataXts), frequency = 6) 对象转换为具有正确开始和结束日期的ts对象,以便使用dataXts函数。在这种情况下,decomposestart = start(dataXts)为我提供了正确的开始和结束日期,但end = end(dataXts)无法识别tseries中的数据列,然后认为所有数据都是数据

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

我不确定我是否能够“xts tsdecompose,但我让library("data.table") # I was unable to read-in using read.table() for some reason.... used fread() as it is much faster data <- fread("DistrBdaily1yrs.txt", header = TRUE, sep="\t") # Set column names to the ones I saw on dropbox, as i was unable to read-in header for some reason! colnames(data) <- c("DATE", "QUANTITY") # Keep as-is data$DATE = as.Date(as.character(data$DATE),format="%Y%m%d") dataXts = xts(data$QUANTITY,data$DATE, frequency = 6) # Not sure what the "QUANTITY" Column means but it must be turned into "numeric" # You can see this post on how to do it if the following is unsatisfactory: # http://stackoverflow.com/questions/3605807/how-to-convert-numbers-with-comma-inside-from-character-to-numeric-in-r a<-as.numeric(gsub(",",".",dataXts)) dataXts <- reclass(a, match.to=dataXts); colnames(dataXts)<- "QUANTITY" # Now convert it to timeSeries timeseries <- ts(dataXts,frequency=6) # decompose decompose(timeseries) 部分正常运作:

xts

此外,当我将ts转换为ts时,我假设它将使用第一个和最后一个日期来构建start = start(dataXts), end = end(dataXts),这就是我遗漏ts()的原因在?ts函数中。另请参阅Dates,因为您无法在startend条件中传递xts,而是:

  

单个数字或两个整数的向量,它们指定时间单位中的自然时间单位和(基于1)个样本数。

您始终可以使用reclass转换回# for example: Say you only want the trend reclass(decompose(timeseries)$trend,match.to=dataXts)

queue.lua