[R] ZOO object colnames refering to Dates

Gabor Grothendieck ggrothendieck at gmail.com
Thu Feb 11 11:12:20 CET 2010


Here are a few more examples:

> ym <- seq(from = as.Date("1999-10-05"), to = as.Date("2000-06-05"),
+ by = "month")
>
> z <- zoo(matrix(seq_len(length(ym) * 5), 5))
> colnames(z) <- format(ym, "X%d.%b.%y")
> z
  X05.Oct.99 X05.Nov.99 X05.Dec.99 X05.Jan.00 X05.Feb.00 X05.Mar.00 X05.Apr.00
1          1          6         11         16         21         26         31
2          2          7         12         17         22         27         32
3          3          8         13         18         23         28         33
4          4          9         14         19         24         29         34
5          5         10         15         20         25         30         35
  X05.May.00 X05.Jun.00
1         36         41
2         37         42
3         38         43
4         39         44
5         40         45
>
> to.header <- function(month, year) {
+ d <- as.Date(paste(year, month, 5, sep = "-"), "%Y-%b-%d")
+     format(d, "X%d.%b.%y")
+ }
>
> to.header("Oct", 1999)
[1] "X05.Oct.99"
>
> for(j in 1:NCOL(z)) print(z[,j])
1 2 3 4 5
1 2 3 4 5
 1  2  3  4  5
 6  7  8  9 10
 1  2  3  4  5
11 12 13 14 15
 1  2  3  4  5
16 17 18 19 20
 1  2  3  4  5
21 22 23 24 25
 1  2  3  4  5
26 27 28 29 30
 1  2  3  4  5
31 32 33 34 35
 1  2  3  4  5
36 37 38 39 40
 1  2  3  4  5
41 42 43 44 45
> for(nm in colnames(z)) print(z[,nm])
1 2 3 4 5
1 2 3 4 5
 1  2  3  4  5
 6  7  8  9 10
 1  2  3  4  5
11 12 13 14 15
 1  2  3  4  5
16 17 18 19 20
 1  2  3  4  5
21 22 23 24 25
 1  2  3  4  5
26 27 28 29 30
 1  2  3  4  5
31 32 33 34 35
 1  2  3  4  5
36 37 38 39 40
 1  2  3  4  5
41 42 43 44 45


On Thu, Feb 11, 2010 at 5:04 AM, Achim Zeileis <Achim.Zeileis at uibk.ac.at> wrote:
> On Thu, 11 Feb 2010, Research wrote:
>
>> Hello,
>>
>> I have  large zoo objects (about 100 or more time series merged next to
>> eachother). Example:
>>
>>   X05.Oct.99 X05.Nov.99 X05.Dec.99 X05.Jan.00 X05.Feb.00 X05.Mar.00
>> X05.Apr.00 X05.May.00 X05.Jun.00
>> [1,]     5649.3     5679.4     5679.4     5679.4     5679.4     5679.4
>> 5679.4     5679.4     5679.4
>> [2,]     5682.7     5719.2     5719.2     5719.2     5719.2     5719.2
>> 5719.2     5719.2     5719.2
>> [3,]     5697.5     5745.5     5745.5     5745.5     5745.5     5745.5
>> 5745.5     5745.5     5745.5
>> [4,]     5723.9     5767.8     5767.8     5767.8     5767.8     5767.8
>> 5767.8     5767.8     5767.8
>> [5,]     5782.1     5829.8     5829.8     5829.8     5829.8     5829.8
>> 5829.8     5829.8     5829.8
>> [6,]     5815.0     5850.9     5850.9     5850.9     5850.9     5850.9
>> 5850.9     5850.9     5850.9
>>>
>>
>> The column names contain dates, i.e.,  X05.Oct.99 stands for a time
>> sequence that was obtained on 05/10/1999. Each column is obtained  month + 1
>> from the previous one.
>>
>> Is it possible to access these columns (time series data) via a for/next
>> loop (or a while) that uses dates i/o (i in 1:8) in the above example?
>>
>> Say: if month==Oct and year==1999 choose column X05.Oct.99 ...
>
> You can switch back and forth between character representations and timedate
> representations using strptime() and format() along with formatting strings,
> e.g.,
>
> R> strptime("X05.Oct.99", "X%d.%b.%y")
> [1] "1999-10-05"
>
> R> format(strptime("X05.Oct.99", "X%d.%b.%y"), "X%d.%b.%y")
> [1] "X05.Oct.99"
>
> R> format(strptime("X05.Oct.99", "X%d.%b.%y"), "%d/%m/%Y")
> [1] "05/10/1999"
>
> And so on. See
>
>  Grothendieck G, Petzoldt T (2004). "R Help Desk: Date and Time
>  Classes in R." R News, 4(1), 29­32.
>  URL http://www.R-project.org/doc/Rnews/Rnews_2004-1.pdf
>
> for more details.
>
> I wonder why you have the time information in the columns rather than in the
> rows though... If your measurements on that day represent intra-day
> measurements, simply use some time/date class, e.g., POSIXct (or chron or
> timeDate) for that. If the measurements correspond to some replications they
> can just go into the columns rather than the rows of the series.
>
> For examples in zoo, see the vignettes of the package.
>
> hth,
> Z
>
>> Could I also generate automatically a variable, appropriately named from
>> the corresponding columns name that would contain the data and access it by
>> that date?
>>
>> Any pointers really appreciated.
>>
>> Thanks in advance,
>> Costas
>>
>>
>> __________ Information from ESET Smart Security, version of virus
>> signature database 4857 (20100211) __________
>>
>> The message was checked by ESET Smart Security.
>>
>> http://www.eset.com
>>
>> ______________________________________________
>> 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.
>
> ______________________________________________
> 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