0
votes

I have a data set stored as follows (only the first 3 rows of data):

My data

The real data set extends until the year 2010. In other words, I have 20 years of data - 240 data points - stored in a matrix-like form whose lines are the years and the columns are the months from January to December.

I would like to know how to convert this data into a time series object, with the first occurrence taking place in Jan. 1990, and the last in Dec. 2010.

I tried:

myData.st = ts(myData, start = c(1990,1), end = c(2010,12), frequency = 12)

but it didn't work correctly. It seems that if I execute the line above, I'll have 20 independent time series objects, each one with only 12 data points.

I want one single time-series object from Jan. 1990 to Dec.2010. One of the main difficulties for me is how to "let R know" that after one row ends in December of a given year, the next value should be taken from January of the next year.

I have found examples of how to convert a data frame into a time series object. However, the data frames in the examples I came across would have a specific column which to convert. Not in this case. Here, all columns have data points of interest.

How can I accomplish that? Would I be able to accomplish this with either the zoo or the xls packages?

Thank you.

1
Please read minimal reproducible example and please do not provide images as no one can copy them to their session.G. Grothendieck

1 Answers

0
votes

It depends on what you have. If it is an R matrix like this then:

m <- matrix(1:240, 12) # test input
ts(c(m), start = 1990, freq = 12)

or if you have an R data frame like this:

DF <- as.data.frame(m)

then convert it to a matrix, m <- as.matrix(DF), and then use the code shown.

Note that print will show such a time series with months along the top and years along the side but it is not two dimensional -- that is just how it prints.

The output is:

     Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1990   1   2   3   4   5   6   7   8   9  10  11  12
1991  13  14  15  16  17  18  19  20  21  22  23  24
1992  25  26  27  28  29  30  31  32  33  34  35  36
1993  37  38  39  40  41  42  43  44  45  46  47  48
1994  49  50  51  52  53  54  55  56  57  58  59  60
1995  61  62  63  64  65  66  67  68  69  70  71  72
1996  73  74  75  76  77  78  79  80  81  82  83  84
1997  85  86  87  88  89  90  91  92  93  94  95  96
1998  97  98  99 100 101 102 103 104 105 106 107 108
1999 109 110 111 112 113 114 115 116 117 118 119 120
2000 121 122 123 124 125 126 127 128 129 130 131 132
2001 133 134 135 136 137 138 139 140 141 142 143 144
2002 145 146 147 148 149 150 151 152 153 154 155 156
2003 157 158 159 160 161 162 163 164 165 166 167 168
2004 169 170 171 172 173 174 175 176 177 178 179 180
2005 181 182 183 184 185 186 187 188 189 190 191 192
2006 193 194 195 196 197 198 199 200 201 202 203 204
2007 205 206 207 208 209 210 211 212 213 214 215 216
2008 217 218 219 220 221 222 223 224 225 226 227 228
2009 229 230 231 232 233 234 235 236 237 238 239 240