I am a bit puzzled here and must be missing something quite basic. I want to extract columns from a multiple time series object. It can perhaps be done by making the ts object into a dataframe and then extracting them, but there is a direct way of subsetting time series as given 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 on running the code in ?ts help, with or without the names parameter gives
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"))
Main question: 1. How does one assign names to columns in time series objects ts and mts ? 2. What are the methods of extracting columns directly from time series object along with the time index? Is it necessary to convert it to zoo or xts class? or add the time index separately?
To give an idea of problem I am 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 extract the dataset for which the indicator is 1, with the time index, I am not sure how to proceed. str(signal) is an mts object but printing signal shows no time index.
Thanks a lot.
Columns (really column names) being referred to as field names (common for each row/record in the table). Then a field refers to a single storage location in a specific record (like a cell) to store one value (the field value).
"mts"
objectYou want colnames()
:
> colnames(z)
[1] "x1" "x2" "x3"
This is because z
is actually a matrix with extra attributes and matrices don't have names
but they do have colnames
.
"mts"
objectTo assign colnames
after the fact or to change them use the replacement function 'colnames<-'
> colnames(z) <- paste0("a", 1:3)
> colnames(z)
[1] "a1" "a2" "a3"
"mts"
objectAs for extracting columns, [
works just fine for "ts"
and "mts"
objects. for example:
> 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
....
For the last bit, I'm not sure what you hope to get. A "ts"
or "mts"
object is a regular time series and extracting the bits of signal[, "d"]
results in a vector not a time series.
> signal[signal[,2] == 1, 1]
[1] 761 1104 810 653 522 956 593
There isn't any time index because this is no longer a "ts"
object. If you want to do this, the zoo package is probably the way you want to go. Here is an example, where we convert to zoo object using as.zoo()
require(zoo)
sz <- as.zoo(signal)
Then we can extract the observations we want (the names on which 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 subsetting call but using the index()
to return the time index of the entire zoo object and then select out the bits we want
> index(sz)[sz[, "i"] == 1]
[1] 1975.917 1976.083 1976.917 1977.917 1978.000 1978.917
[7] 1979.000
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With