[R] Plotting Time Series

Prof Brian D Ripley ripley at stats.ox.ac.uk
Thu May 25 07:28:32 CEST 2000


On 24 May 2000, John Davis wrote:

> I'm new to R and reading Modern Applied Statistics w/ S-Plus (read the
> first two chapters and am skimming the chapter 13). I'm trying to take
> data of the form:
> 
> Time Attribute Frequency
> 1       A       7
> 1       B       12
> 2       A       6
> 2       C       4
> 2       D       7
> 4       B       11
> 4       D       5
> 5       A       9
> 5       B       11
> 5       C       14
>    [...]
> 
> and create a time series (Time ~ Frequency) for each attribute in the
> same plot somewhat similar to Figure 13.1 on page 403 (which plot I
> can reproduce following ch13.R).

BTW, Time ~ Frequency has a technical meaning in R, and you don't mean it
in that sense. It's in Chapter 6, one of the ones you skipped.
And most people would read that to mean Time as the response and Frequency
the time base, which I think is the reverse of what you mean.

> There are a few ways I've thought to address this:
> 
> 1. Rearrange the data into one file for each attribute
> 
> eg.
> 
> File_A
> 1 7
> 2 6
> 5 9
> 
> Then plot each in the same plot...
> 
> 2. Read the data from one file, then use the factors to create vectors
>    of different lengths holding the time and freq data.
> 
> 3. Divine some magic about factors and do it all with some incredible
>    one liner.
> 
> The last is where I'd like to get to, but I need a better mental model
> of what is going on... Presently, I'm just "trying stuff". Anyway, any
> clues would be appreciated, about the problem itself, or about the
> "trying stuff" approach to R.

Unfortunately, your example is not a time series in the sense used in R.
That must have a regularly-spaced time base.  You have what S-PLUS
calls an `irregular time series' (class its) and little support for.

You could plot the series by

plot(Time[Attribute==A], Frequency[Attribute==A])

for example.  Indexing is a very powerful tool in the S language.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list