Re: [R] cbind'ing multivariate ts objects

From: Андрей Парамонов <cmr.pent_at_gmail.com>
Date: Sun, 15 Jun 2008 11:13:40 +0400

2008/6/15 jim holtman <jholtman_at_gmail.com>:
> Is this what you want:
>
>> t <- ts(data.frame(a = 10:20, b = 20:30, c = 30:40, d = 40:50))
>> t1 <- t[, c('a', 'b')]
>> t2 <- t[, c('c', 'd')]
>>
>> colnames(t1)
> [1] "a" "b"
>>
>> colnames(t2)
> [1] "c" "d"
>>
>> x <- cbind(t1, t2)
>> colnames(x)
> [1] "t1.a" "t1.b" "t2.c" "t2.d"
>> colnames(x) <- sub("^.*\\.", "", colnames(x))
>> x
> Time Series:
> Start = 1
> End = 11
> Frequency = 1
> a b c d
> 1 10 20 30 40
> 2 11 21 31 41
> 3 12 22 32 42
> 4 13 23 33 43
> 5 14 24 34 44
> 6 15 25 35 45
> 7 16 26 36 46
> 8 17 27 37 47
>

Not really. This is just a workaround, which doesn't work always work:

> t <- ts(data.frame('a.a' = 10:20, b = 20:30, c = 30:40, d = 40:50))
> t1 <- t[, c('a.a', 'b')]
> t2 <- t[, c('c', 'd')]
> colnames(t1)
[1] "a.a" "b"
> colnames(t2)

[1] "c" "d"
> s <- cbind(t1, t2)
> colnames(s)

[1] "t1.a.a" "t1.b" "t2.c" "t2.d"
> sub("^.*\\.", "", colnames(s))

[1] "a" "b" "c" "d"

I have found that zoo's merge() does exactly what I want. Should I stick to zoo library or there is some equivalent of zoo's merge() for ts objects?

Thanks,
Andrey Paramonov



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Sun 15 Jun 2008 - 07:43:49 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 15 Jun 2008 - 08:30:43 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive