简体   繁体   中英

Conversion to time Series data in R

I am using the following code to convert the data into ts format:

unemployment<-ts(as.vector(t(as.matrix(cleandata1))),start = c(1948,1), end=c(2015,12), frequency=12)

And I get:

        Jan    Feb    Mar    Apr    May    Jun
1948 1948.0    3.4    3.8    4.0    3.9    3.5
1949    4.0 1949.0    4.3    4.7    5.0    5.3
1950    6.4    6.6 1950.0    6.5    6.4    6.3
1951    4.2    4.2    4.3 1951.0    3.7    3.4
1952    3.3    3.5    3.5    3.1 1952.0    3.2
1953    3.4    3.1    3.0    2.8    2.7 1953.0
1954    2.6    2.7    2.9    3.1    3.5    4.5...till Dec 2015

Original data format looks like:

Year  Jan  Feb  Mar  Apr  May  Jun Jul Aug
1  1948  3.4  3.8  4.0  3.9  3.5  3.6 3.6 3.9
2  1949  4.3  4.7  5.0  5.3  6.1  6.2 6.7 6.8
3  1950  6.5  6.4  6.3  5.8  5.5  5.4 5.0 4.5
4  1951  3.7  3.4  3.4  3.1  3.0  3.2 3.1 3.1
5  1952  3.2  3.1  2.9  2.9  3.0  3.0 3.2 3.4
6  1953  2.9  2.6  2.6  2.7  2.5  2.5 2.6 2.7
7  1954  4.9  5.2  5.7  5.9  5.9  5.6 5.8 6.0
8  1955  4.9  4.7  4.6  4.7  4.3  4.2 4.0 4.2
9  1956  4.0  3.9  4.2  4.0  4.3  4.3 4.4 4.1....till Dec 2015

Can anyone please suggest the correct code?

It seems you read the original/raw data into data.frame and it has already column names equalling to month names (Jan, Feb, etc.) and row names equalling to years (1948, ...).

Hoping, no missings in your data, the following code should convert it to ts format, without any errors:

unemployment <- ts(cleandata1[-1,-1], start=c(1948,1), freq=12)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM