[R] How can I tabulate time series data (in RStudio or any other R editor)?
David Winsemius
dwinsemius at comcast.net
Fri Feb 17 14:46:55 CET 2012
On Feb 16, 2012, at 8:32 PM, jpm miao wrote:
> Hello,
>
> I have a question on how to tabulate the time series data. I use
> RStudio, but if can be done in any other R editor, it should work in
> RStudio as well.
Most people have given up on using R's time series class and have
moved over to using the zoo-class. There are no real dates in time
series objects. Things that look like dates in printed output are
calculated on the fly. The data is really just a vector and the
"dates" are contained in the attributes of the object.
>
>> a1<-11:22
>
>> a1ts<-ts(a1, frequency=4, start=c(1978,1))
>
>> a1ts Qtr1 Qtr2 Qtr3 Qtr4
> 1978 11 12 13 14
> 1979 15 16 17 18
> 1980 19 20 21 22
>
>
> If I click the variable "a1ts" on the workspace, I see
>
> structure(11:22, .Tsp = c(1978, 1980.75, 4), class = "ts")
>
> If I coerce the variable to become a matrix,
>
>> a1tsm<-as.matrix(a1ts)
>
>
> and click the variable "a1tsm", I see the vector in a tabular
> form and
> can paste it into Excel , but I don't see the dates at all
> V1
> 1 11
> 2 12
> 3 13
> 4 14
> 5 15
> 6 16
> 7 17
> 8 18
> 9 19
> 10 20
> 11 21
> 12 22
>
> How could I see both the dates and the data in a tabular form?
Not really a full answer but this will generate a time-series object
of "times" although they are not really any of the usual R time or
date classes.
> time(a2ts)
Qtr1 Qtr2 Qtr3 Qtr4
1979 1979.00 1979.25 1979.50 1979.75
1980 1980.00 1980.25 1980.50 1980.75
1981 1981.00 1981.25 1981.50 1981.75
1982 1982.00 1982.25 1982.50 1982.75
>
> The second question is that if I have another data sequence, how
> can I
> combine the two and see both data in a tabular form?
>
>> a2<-101:116> a2ts<-ts(a2, frequency=4, start=c(1979,1))> a2ts
>> Qtr1 Qtr2 Qtr3 Qtr4
> 1979 101 102 103 104
> 1980 105 106 107 108
> 1981 109 110 111 112
> 1982 113 114 115 116
>
You can't. The data in a time series is a vector, not a dataframe or a
matrix. If you want the data to be a more full-featured data object
then use zoo-classed objects which allow matrices and allow real
dates or times.
install.packages("zoo")
>>
> Thank you,
>
> Miao
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list