[R] Zoo object problem: Find the column name of a univariate zoo object
R. Michael Weylandt
michael.weylandt at gmail.com
Fri Aug 10 08:29:53 CEST 2012
On Thu, Aug 9, 2012 at 9:54 PM, jpm miao <miaojpm at gmail.com> wrote:
> Hi Michael,
> This is my problem. Could you find out how I can find the column name of
> the zoo object "xzoo"? I am expecting "EUR".
>
> Thanks,
>
> Miao
>
>
>
>> x<-read.csv("A_FX_EUR_Q.csv", header=TRUE)
>> x[1:4,]
> TIME EUR
> 1 198001 1.41112
> 2 198002 1.39108
> 3 198003 1.42323
> 4 198004 1.34205
>> xzoo<-zoo(x[,2:NCOL(x)], seq(as.yearqtr(1980+(1-1)/4),
>> as.yearqtr(2012+(2-1)/4), 1/4))
>> xzoo[1:4,]
> 1980 Q1 1980 Q2 1980 Q3 1980 Q4
> 1.41112 1.39108 1.42323 1.34205
>> NCOL(xzoo)
> [1] 1
>> colnames(xzoo)
> NULL
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
>
>
> 2012/8/10 R. Michael Weylandt <michael.weylandt at gmail.com>
> <michael.weylandt at gmail.com>
>
>>
>>
>> On Aug 9, 2012, at 8:32 PM, jpm miao <miaojpm at gmail.com> wrote:
>>
>> > Hi everyone and Achim,
>> >
>> > Achim, I appreciate your help about the function "NCOL". When I use
>> > "NCOL" instead of "ncol", I can find out the number of columns (number
>> > of
>> > time series) in the presence of only one time series (one variable, one
>> > column).
>> >
>> > Now I want to know how I can find out the column names of the zoo
>> > objects? In case of more than one time series, the function "colnames"
>> > works, but not for the univariate time series.
>> >
>>
>> 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
>>
>>
>> > Thanks,
>> >
>> > Miao
>> >
>> > 2012/8/9 Achim Zeileis <Achim.Zeileis at uibk.ac.at>
>> >
>> >> On Thu, 9 Aug 2012, jpm miao wrote:
>> >>
>> >> Hi,
>> >>>
>> >>> Part of my program is to calculate the number of time series in a zoo
>> >>> object. It works well if it has more than one time series, but it
>> >>> fails if
>> >>> it has only one. How can I access the number of column (i.e. the
>> >>> number of
>> >>> time series) when I have only one column? Why is the number of an
>> >>> object
>> >>> of
>> >>> only one object "NULL"? It should be one, shouldn't it? (The following
>> >>> example does not involve the creation of a zoo object; in reality,
>> >>> similar
>> >>> problems are encountered when I create a zoo object)
>> >>>
>> >>
>> >> Univariate zoo series are by default vectors, i.e., have no "dim"
>> >> attribute. You can either choose to store a matrix instead of a vector
>> >> in
>> >> the zoo series - or you can use NCOL() instead of ncol() to extract the
>> >> number of columns. See the corresponding manual pages for more details.
>> >> An
>> >> illustration is included below:
>> >>
>> >> ## univariate series
>> >>
>> >> R> x <- sin(1:4)
>> >> R> z <- zoo(x)
>> >> R> z
>> >> 1 2 3 4
>> >> 0.8414710 0.9092974 0.1411200 -0.7568025
>> >> R> dim(z)
>> >> NULL
>> >> R> ncol(z)
>> >> NULL
>> >> R> NCOL(z)
>> >> [1] 1
>> >>
>> >> ## alternatives to create a 1-column matrix instead of a vector
>> >>
>> >> R> z1 <- zoo(matrix(x, ncol = 1))
>> >> R> z1
>> >>
>> >> 1 0.8414710
>> >> 2 0.9092974
>> >> 3 0.1411200
>> >> 4 -0.7568025
>> >> R> dim(z1)
>> >> [1] 4 1
>> >>
>> >> R> dim(z) <- c(NROW(z), NCOL(z))
>> >> R> z
>> >>
>> >> 1 0.8414710
>> >> 2 0.9092974
>> >> 3 0.1411200
>> >> 4 -0.7568025
>> >> R> dim(z)
>> >> [1] 4 1
>> >>
>> >>
>> >> temp5<-read.csv("A_**Consumption.csv", header=TRUE)> temp5[1:3,]
>> >> TIME
>> >>>> C C_D C_ND
>> >>>>
>> >>> 1 196101 70345 1051 69294
>> >>> 2 196102 61738 905 60833
>> >>> 3 196103 63838 860 62978> temp6<-temp5[,2:ncol(temp5)]> temp6[1:3,]
>> >>> C C_D C_ND
>> >>> 1 70345 1051 69294
>> >>> 2 61738 905 60833
>> >>> 3 63838 860 62978> colnames(temp6)[1] "C" "C_D" "C_ND">
>> >>> temp7<-read.csv("A_FX_EUR_Q.**csv", header=TRUE)> temp7[1:3,] TIME
>> >>> EUR
>> >>> 1 198001 1.41112
>> >>> 2 198002 1.39108
>> >>> 3 198003 1.42323> temp8<-temp7[,2:ncol(temp7)]> temp8[1:3,]Error in
>> >>> temp8[1:3, ] : incorrect number of dimensions
>> >>>
>> >>> ncol(temp6)[1] 3> ncol(temp8) # Why isn't it 1?NULL
>> >>>>
>> >>>
>> >>> temp8 [1] 1.411120 1.391080 1.423230 1.342050 1.232870
>> >>>>
>> >>> [6] 1.115090 1.032930 1.089250 1.036320 1.001850
>> >>> [11] 0.950641 0.933593 0.947940 0.911204 0.860682
>> >>> [16] 0.843864 0.831666 0.824776 0.768626 0.732064
>> >>> [21] 0.684473 0.726018 0.784729 0.852911 0.922885
>> >>> [26] 0.958778 1.012740 1.038160 1.124550 1.149780
>> >>> [31] 1.128450 1.214120 1.233530 1.216270 1.113620
>> >>> [36] 1.170250 1.126230 1.074330 1.078480 1.127870
>> >>> [41] 1.205540 1.222740 1.296500 1.366550 1.341280
>> >>> [46] 1.187600 1.176790 1.254490 1.262610 1.271820
>> >>> [51] 1.385930 1.268130 1.190480 1.206840 1.150270
>> >>> [56] 1.140010 1.125200 1.163450 1.226830 1.240210
>> >>> [61] 1.273300 1.331010 1.312420 1.317350 1.287330
>> >>> [66] 1.254500 1.274210 1.261930 1.178970 1.143500
>> >>> [71] 1.093320 1.123400 1.086770 1.100380 1.117670
>> >>> [76] 1.176960 1.121600 1.056900 1.048600 1.038000
>> >>> [81] 0.986500 0.933200 0.905200 0.868300 0.923200
>> >>> [86] 0.872500 0.890300 0.895900 0.876600 0.918800
>> >>> [91] 0.983800 0.999400 1.073100 1.137200 1.124800
>> >>> [96] 1.189000 1.249700 1.204600 1.222000 1.297700
>> >>> [101] 1.311300 1.259400 1.219900 1.188400 1.202300
>> >>> [106] 1.258200 1.274300 1.288700 1.310600 1.348100
>> >>> [111] 1.373800 1.448600 1.497600 1.562200 1.505000
>> >>> [116] 1.318000 1.302900 1.363200 1.430300 1.477900
>> >>> [121] 1.382900 1.270800 1.291000 1.358300 1.368000
>> >>> [126] 1.439100 1.412700 1.348200 1.310800 1.281400
>> >>>
>> >>> [[alternative HTML version deleted]]
>> >>>
>> >>> ______________________________**________________
>> >>> R-help at r-project.org mailing list
>> >>>
>> >>> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help>
>> >>> PLEASE do read the posting guide http://www.R-project.org/**
>> >>> posting-guide.html <http://www.R-project.org/posting-guide.html>
>> >>> and provide commented, minimal, self-contained, reproducible code.
>> >>>
>> >>>
>> >
>> > [[alternative HTML version deleted]]
>> >
>> > ______________________________________________
>> > 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.
>
>
More information about the R-help
mailing list