[R] Representing 'Date' as 'Year - Quarter'
Gabor Grothendieck
ggrothendieck at gmail.com
Sat May 31 13:28:50 CEST 2008
The zoo package has a yearqtr class that can represent quarterly
data. It is also possible to create zoo series of quarterly data. Such
series can be converted to ts class and that class can some
builtin support for quarterly data as well.
> library(zoo)
> as.yearqtr("200706", "%Y%m")
[1] "2007 Q2"
The read.zoo function has an aggregate= argument that can aggregate
values at duplicate times into a single value as they are read in; however,
it assumes one record per time point and it appears your data is the
transpose so you may wish to read it in using read.table and then
transpose it manually or creating separate series and then using merge.zoo
to bind them into one multivariate series.
See the corresponding help pages and the three vignettes.
On Sat, May 31, 2008 at 4:39 AM, Vishal Belsare
<vishal.belsare at gmail.com> wrote:
> I have financial data on a a set of firms, with a quarterly period
> (fundamental data). The data spans 10 years, and four quarters per
> year. The present file (.csv) reads the Date columns as "200706" for
> the second quarter of 2007; "199809" for the third quarter of 1997.
>
> Is there a way I can convert it to something like "2007 Q2", "1998 Q3"?
>
> I am aware of the yearqtr feature of the zoo package and ts also has
> some facility to represent frequency.
>
> However, the problem is that, my data isn't exactly a time series
> right now. In each quarterly file, the date column has the same value
> f.e 200706, however each row contains fundamental data of a different
> firm.
>
> Ultimately, I intend to aggregate the 40 or so files into one data frame.
>
> So is it possible to read off the 199806 as a 'date' variable, with a
> quarterly frequency? The reason why I am keen on this is that I can
> then shape my further sub-setting in a form like which(... date ==
> "1998 Q2") or so instead of forcing myself to write "1998/06/30" which
> would indicate end of second quarter (I just think that is redundant).
>
> Thanks in anticipation.
We don't have a sample of your data to know for sure so lets just
assume that column 1 is the year/quarter and the rest of the line
is the data one quarter after another. We read it in and convert
column 1 to yearqtr class. Then we lapply over the rows converting
each one to a zooreg series and clean up its names. Then we merge
them all into a single zoo series and optionally convert that to ts:
# untested
DF <- read.table("myfile")
DF$V1 <- as.yearqtr(DF$V1, "%Y%m")
zl <- lapply(1:nrow(DF), function(i) zooreg(unlist(DF[i, 2:ncol(DF)]),
start = DF[i, 1], freq = 4))
names(z) <- row.names(DF)
z <- do.call(merge, zl)
# optional
as.ts(z)
See the corresponding help entries in the zoo package and the three
vignettes accompanying the package.
More information about the R-help
mailing list