Skip to content

Zoo object problem: Find the column name of a univariate zoo object

7 messages · David Winsemius, michael.weylandt at gmail.com (R. Michael Weylandt, John +2 more

#
On Aug 9, 2012, at 6:32 PM, jpm miao wrote:

            
>  x <- sin(1:4)
 > names(x) <- letters[1:4]
 > z <- zoo(x)
 > names(z)
[1] "a" "b" "c" "d"
On Aug 9, 2012, at 8:32 PM, jpm miao <miaojpm at gmail.com> wrote:

            
That's not true. zoo allows (but does not require) the data to have a dim attribute, as with any other vector. If (and I believe only if) an object has a dim attribute and hence can have colnames. For example, see xts which is a subclass of zoo requiring dim and for which colnames and names are equivalent. 

Michael
#
On Aug 9, 2012, at 7:54 PM, jpm miao wrote:

            

        
To repeat what Michael wrote:

            
I think Michael might be wrong here. Note that the error message  from  
`colnames<-` also adds extra requirements:
>  colnames(xzoo) <- "test"
Error in `colnames<-`(`*tmp*`, value = "test") :
   attempt to set colnames on object with less than two dimensions

Note that there does not seem to be a "names<-.zoo" method that offers  
any improvement, although no error is extended.

 > names(xzoo) <- "test"
 > names(xzoo)
NULL
#
On Thu, Aug 9, 2012 at 9:54 PM, jpm miao <miaojpm at gmail.com> wrote:
I believe the constructor you want (use dput next time as I've asked
you before!) is something like

zoo(x[,-1, drop = FALSE], order.by = yearqtr(1980+ seq(0.25, 1,
length.out = 4)))

giving, e.g.,

xzoo <- structure(c(1.33001066655852, 1.31852671261877, 1.32210723729804,
1.3078194887843), .Dim = c(4L, 1L), .Dimnames = list(NULL, "EUR"),
index = structure(c(1980.25,
1980.5, 1980.75, 1981), class = "yearqtr"), class = "zoo")

which has names and colnames equal (And changing one changes the
other) because drop = FALSE preserves the dimensionality even in this
one column case.

Michael
#
HI,

I guess names() work if the zoo object has columns.

(https://stat.ethz.ch/pipermail/r-help/2006-November/117448.html)
x <- sin(1:4)
library(zoo)
?x2<-zoo(as.matrix(x))
names(x2)<-"test"
?names(x2)
#[1] "test"
?x2
??????? test
1? 0.8414710
2? 0.9092974
3? 0.1411200
4 -0.7568025

x3<-zoo(x)
?names(x3)<-"test"
?names(x3)
#NULL
A.K.




----- Original Message -----
From: David Winsemius <dwinsemius at comcast.net>
To: jpm miao <miaojpm at gmail.com>
Cc: R-help at r-project.org; Achim Zeileis <Achim.Zeileis at uibk.ac.at>
Sent: Friday, August 10, 2012 1:18 AM
Subject: Re: [R] Zoo object problem: Find the column name of a univariate zoo object
On Aug 9, 2012, at 7:54 PM, jpm miao wrote:

            

        
To repeat what Michael wrote:

            
I think Michael might be wrong here. Note that the error message? from `colnames<-` also adds extra requirements:
Error in `colnames<-`(`*tmp*`, value = "test") :
? attempt to set colnames on object with less than two dimensions

Note that there does not seem to be a "names<-.zoo" method that offers any improvement, although no error is extended.
NULL

--D.
David Winsemius, MD
Alameda, CA, USA

______________________________________________
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.