I use R 2.7.0 on GNU/Linux. I have noticed a problem in cbind method for multivariate time series (ts) objects. Consider the following example:> 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"> colnames(cbind(t1, t2))[1] "t1.a" "t1.b" "t2.c" "t2.d">Although it seems to be the documented behavior, I think it would be more consistent if the latter returned c("a", "b", "c", "d") instead. Is there a nice way to achieve this? Thanks! Andrey Paramonov
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)) > xTime 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 On Sat, Jun 14, 2008 at 1:14 PM, ?????? ????????? <cmr.pent at gmail.com> wrote:> I use R 2.7.0 on GNU/Linux. I have noticed a problem in cbind method > for multivariate time series (ts) objects. Consider the following > example: > >> 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" >> colnames(cbind(t1, t2)) > [1] "t1.a" "t1.b" "t2.c" "t2.d" >> > > Although it seems to be the documented behavior, I think it would be > more consistent if the latter returned c("a", "b", "c", "d") > instead. Is there a nice way to achieve this? 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
On Sat, 14 Jun 2008, ?????? ????????? wrote:> I use R 2.7.0 on GNU/Linux. I have noticed a problem in cbind method > for multivariate time series (ts) objects. Consider the following > example: > >> 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" >> colnames(cbind(t1, t2)) > [1] "t1.a" "t1.b" "t2.c" "t2.d" >> > > Although it seems to be the documented behavior, I think it would be > more consistent if the latter returned c("a", "b", "c", "d")Consistent with what? This is how all such combinations of matrices in R work, and avoids duplicate names.> instead. Is there a nice way to achieve this? Thanks!Use colnames() to change the names -- it is just a matrix. tt <- cbind(t1, t2) colnames(tt) <- c("a", "b", "c", "d")> > 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. >-- 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 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Try the zoo package:> library(zoo) > as.ts(cbind(as.zoo(t1), as.zoo(t2)))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 9 18 28 38 48 10 19 29 39 49 11 20 30 40 50 On Sat, Jun 14, 2008 at 1:14 PM, ?????? ????????? <cmr.pent at gmail.com> wrote:> I use R 2.7.0 on GNU/Linux. I have noticed a problem in cbind method > for multivariate time series (ts) objects. Consider the following > example: > >> 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" >> colnames(cbind(t1, t2)) > [1] "t1.a" "t1.b" "t2.c" "t2.d" >> > > Although it seems to be the documented behavior, I think it would be > more consistent if the latter returned c("a", "b", "c", "d") > instead. Is there a nice way to achieve this? 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. >