[R] Get the names of the columns in a tserie

Gavin Simpson gavin.simpson at ucl.ac.uk
Fri Oct 27 10:54:25 CEST 2006


On Thu, 2006-10-26 at 17:17 -0700, lvdtime wrote:
> Please, I need this information, it's important for my work

By that, do you mean that your time/work is more important than that of
the other members of this list?

Anyway, yes, there is an equivalent of label(X[, i]). You can use
colnames, as in colnames(X)[i] or colnames(X[, i, drop = FALSE]), but it
won't automagically select the column of the time series you have told R
to plot, you'd do it manually:

## dummy data
dat <- matrix(rnorm(100), ncol = 5)
colnames(dat) <- LETTERS[1:5]
dat <- as.ts(dat)
dat
## plot column 3 of the time series
plot(dat[, 3], ylab = colnames(dat)[3])

If you want to automate this, then you need to write a wrapper to
plot.ts, one such, very simple, wrapper might look something like this:

my.plot.ts <- function(x, series) {
	plot(x[, series], ylab = colnames(x)[series])}

my.plot.ts(dat, series = 3)
my.plot.ts(dat, 3) # equivalent of above

Is this what you were looking for?

Actually, having looked at what happens when you subset a "ts" object.
You are selecting a single time series from a multivariate time series,
which follows R's general behaviour of dropping the empty dimension,
e.g., contrast :

dim(dat[, 1])
dim(dat[, 1, drop = FALSE])

This has the side effect of dropping the colnames of the "ts" object:

str(dat[, 1])
str(dat[, 1, drop = FALSE])

So, all you need is to modify your plot call to include drop = FALSE:

plot(dat[, 2, drop = FALSE])

and plot.ts will take care of the labelling for you.

G

> 
> Thank you.
> 
> 
> lvdtime wrote:
> > 
> > Hello everybody,
> > 
> > I'm a beginner in R, and I'm currently working on Tseries (analysis of a
> > portfolio)
> > 
> > I imported the data like this (library tseries) :
> > X<-read.ts("X.dat", start=c(1995,1), frequency=261, header=T, sep=";")
> > 
> > There is a header which contains the names of each column (codes of
> > shares)
> > 
> > I'd like to know if it is possible to get the names of the columns (to
> > display it in the plots : ylab=name of the col)
> > To summarize, I wonder if a code like "label(X[,i])" exists...
> > 
> > 
> > Thank you for your help
> > 
> > 
> > LVDTime
> > 
> > 
> 
-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 *Note new Address and Fax and Telephone numbers from 10th April 2006*
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Gavin Simpson                     [t] +44 (0)20 7679 0522
ECRC                              [f] +44 (0)20 7679 0565
UCL Department of Geography
Pearson Building                  [e] gavin.simpsonATNOSPAMucl.ac.uk
Gower Street
London, UK                        [w] http://www.ucl.ac.uk/~ucfagls/cv/
WC1E 6BT                          [w] http://www.ucl.ac.uk/~ucfagls/
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-help mailing list