How to name columns in time series objects?

I am a little puzzled here and should miss something rather simple. I want to extract columns from an object with multiple time series. This can be done by making the ts object in the data framework and then extracting them, but there is a direct way to subset the time series, as indicated in this question link

To figure out how to assign names to ts objects? ts shows

ts(data = NA, start = 1, end = numeric(), frequency = 1, deltat = 1, ts.eps = getOption("ts.eps"), class = , names = ) 

but when running the code in the help? ts with or without names parameter names names(z) NULL.

z <- ts(matrix(rnorm(300), 100, 3), start=c(1961, 1), frequency=12) or z <- ts(matrix(rnorm(300), 100, 3), start=c(1961, 1), frequency=12, names=c("x1", "x2", "x3"))

The main question: 1. How to assign names to columns in ts and mts time series objects? 2. What are the methods for extracting columns directly from a time series object along with a time index? Do I need to convert it to zoo or xts class? or add time index separately?

To give an idea of ​​the problem I'm trying to solve:

 # using inbuilt ldeaths time series dataset ldeaths d <- diff(ldeaths) percen <- quantile(d, 0.9) i <- ifelse(d>percen, 1,0) signal <- cbind(d,i) 

Now, to retrieve a dataset for which the indicator is 1, with a time index, I'm not sure how to proceed. str (signal) is an mts object, but there is no time index in the print signal.

Many thanks.

+7
source share
2 answers

Retrieving Series Names in the "mts" Object

Do you want colnames() :

 > colnames(z) [1] "x1" "x2" "x3" 

This is because z is actually a matrix with additional attributes, and the matrices do not have names , but they have colnames .

Assigning / changing series names in the "mts" object

To assign colnames after the fact or change them, use the replace function 'colnames<-'

 > colnames(z) <- paste0("a", 1:3) > colnames(z) [1] "a1" "a2" "a3" 

Retrieving a specific series from the "mts" object

As for extracting columns, [ works great for objects "ts" and "mts" . eg:

 > z[,1] Jan Feb Mar Apr May 1961 0.81800833 -0.30852155 0.05915071 0.14937058 0.67734362 1962 1.12993606 -0.81176485 -0.51903387 1.12527537 -0.34377553 1963 1.30469813 0.32486340 0.01029512 -1.13631688 -1.22013150 1964 0.72449621 -0.88704234 0.78834391 -0.92956537 -0.31584252 1965 0.24610412 0.97980266 0.17136276 2.45216318 0.15846038 1966 -0.48891587 -0.62820331 0.33190472 2.14094813 1.32389152 1967 0.49120472 -0.10149521 -0.39070688 -0.78743955 -1.20563040 1968 -0.70749150 0.52333087 -0.51991721 0.02037504 -0.59848254 1969 -0.80156968 -1.38172513 0.09400527 0.66966443 .... 

Feelings problems OP

For the last bit, I'm not sure what you hope to get. The "ts" or "mts" object is a regular time series, and extracting the signal[, "d"] bits results in a vector, not a time series.

 > signal[signal[,2] == 1, 1] [1] 761 1104 810 653 522 956 593 

There is no time index because it is no longer a "ts" object. If you want to do this, the zoo package is likely to be what you want. Here is an example where we convert a zoo object using as.zoo()

 require(zoo) sz <- as.zoo(signal) 

Then we can extract the observations we need (names that are a useful indicator of the time index)

 > sz[sz[, "i"] == 1, "d"] 1975(12) 1976(2) 1976(12) 1977(12) 1978(1) 1978(12) 1979(1) 761 1104 810 653 522 956 593 

and then a similar call to a subset, but using index() to return the time index of the entire zoo object, and then select the bits we need

 > index(sz)[sz[, "i"] == 1] [1] 1975.917 1976.083 1976.917 1977.917 1978.000 1978.917 [7] 1979.000 
+9
source

If you want to rename the column of objects mts (matrix time series). Just use the colnames() function.

For example: you have time series data containing three columns, and you want to change this column name:

 colnames(data) <- c('newname1','newname2','newname3') 

Hope this helps.

0
source

All Articles