Surbhi Bhatia
Surbhi Bhatia

Reputation: 1

Ordering zoo object by months - sequentially

I have a data frame looks like this:

month        SYMBOL val1 val2 val3  
Jan/2017        A   3   4   6  
Feb/2017        A   1   2   4  
Mar/2017        A   2   5   3  
Apr/2017        A   4   3   6  
May/2017        A   6   2   8  
Jan/2017        B   7   3   1  
Feb/2017        B   3   7   3  
Mar/2017        B   1   3   6  
Apr/2017        B   7   2   8  
May/2017        B   9   7   2  
Jan/2017        C   0   8   6  
Feb/2017        C   1   3   9  
Mar/2017        C   3   3   1  
Apr/2017        C   4   1   5  
May/2017        C   6   7   1  

When I convert it into a zoo object with yearmon as index, the SYMBOLS column changes like this:

    SYMBOL val1 val2 val3  
Jan/2017    A   3   4   6  
Jan/2017    B   7   3   1    
Jan/2017    C   0   8   6  
Feb/2017    A   1   2   4  
Feb/2017    B   3   7   3  
Feb/2017    C   1   3   9  
Mar/2017    A   2   5   3  
Mar/2017    B   1   3   6       
Mar/2017    C   3   3   1   
Apr/2017    A   4   3   6     
Apr/2017    B   7   2   8     
Apr/2017    C   6   2   8     
May/2017    A   9   7   2  
May/2017    B   4   1   5   
May/2017    C   6   7   1  

Is there a way to order years sequentially while creating a zoo object so that the SYMBOLS remain as AAA, BBB, CCC instead of getting distorted? zoo inevitably changes it to JAN JAN JAN FEB FEB FEB instead of JAN - MAY for symbol A, JAN - MAY for symbol B and so on.

Upvotes: 0

Views: 112

Answers (1)

G. Grothendieck
G. Grothendieck

Reputation: 270195

A zoo object is a timeseries and, in particular, time series have ordered observations. If you want to represent an object that is not a time series then either don't use zoo or somehow re-work it into being a time series.

1) Multivariate time series Although the data as presented (see Lines in the Note below) is not a time series it can be represented as a multivariate time series by splitting it on the second input column:

library(zoo)
z <- read.zoo(text = Lines, split = 2, FUN = as.yearmon, format = "%b/%Y", header = TRUE)

giving:

> z
         val1.A val2.A val3.A val1.B val2.B val3.B val1.C val2.C val3.C
Jan 2017      3      4      6      7      3      1      0      8      6
Feb 2017      1      2      4      3      7      3      1      3      9
Mar 2017      2      5      3      1      3      6      3      3      1
Apr 2017      4      3      6      7      2      8      4      1      5
May 2017      6      2      8      9      7      2      6      7      1

2) by list of multiple time series Alternately, it would also be possible to represent it as a by list of zoo objects:

DF <- read.table(text = Lines, header = TRUE)
byz <- by(DF[-2], DF[2], function(x) read.zoo(x, FUN = as.yearmon, format = "%b/%Y"))

giving:

> byz
SYMBOL: A
         val1 val2 val3
Jan 2017    3    4    6
Feb 2017    1    2    4
Mar 2017    2    5    3
Apr 2017    4    3    6
May 2017    6    2    8
------------------------------------------------------------ 
SYMBOL: B
         val1 val2 val3
Jan 2017    7    3    1
Feb 2017    3    7    3
Mar 2017    1    3    6
Apr 2017    7    2    8
May 2017    9    7    2
------------------------------------------------------------ 
SYMBOL: C
         val1 val2 val3
Jan 2017    0    8    6
Feb 2017    1    3    9
Mar 2017    3    3    1
Apr 2017    4    1    5
May 2017    6    7    1

3) synthesized index It may be difficult to manipulate such an object but to cover all the possibilities one could synthesize a new index from the SYMBOL and month columns to create a zoo series with a character index like this.

myindex <- function(sym, mon) paste(sym, format(as.yearmon(mon, "%b/%Y"), "%Y-%m"))
z2 <- read.zoo(text = Lines, index = 2:1, FUN = myindex, header = TRUE)

giving the following zoo object:

> z2
          val1 val2 val3
A 2017-01    3    4    6
A 2017-02    1    2    4
A 2017-03    2    5    3
A 2017-04    4    3    6
A 2017-05    6    2    8
B 2017-01    7    3    1
B 2017-02    3    7    3
B 2017-03    1    3    6
B 2017-04    7    2    8
B 2017-05    9    7    2
C 2017-01    0    8    6
C 2017-02    1    3    9
C 2017-03    3    3    1
C 2017-04    4    1    5
C 2017-05    6    7    1

Note: The input in reproducible form is:

Lines <- "month SYMBOL val1 val2 val3
Jan/2017 A 3 4 6
Feb/2017 A 1 2 4
Mar/2017 A 2 5 3
Apr/2017 A 4 3 6
May/2017 A 6 2 8
Jan/2017 B 7 3 1
Feb/2017 B 3 7 3
Mar/2017 B 1 3 6
Apr/2017 B 7 2 8
May/2017 B 9 7 2
Jan/2017 C 0 8 6
Feb/2017 C 1 3 9
Mar/2017 C 3 3 1
Apr/2017 C 4 1 5
May/2017 C 6 7 1"

Upvotes: 1

Related Questions