简体   繁体   English

在R中转换为时间序列数据

[英]Conversion to time Series data in R

I am using the following code to convert the data into ts format: 我正在使用以下代码将数据转换为ts格式:

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, ...). 看来您将原始/原始数据读入了data.frame,并且它已经具有等于月份名称(Jan,Feb等)的列名称和等于年份(1948,...)的行名称。

Hoping, no missings in your data, the following code should convert it to ts format, without any errors: 希望您的数据中没有遗漏,以下代码应将其转换为ts格式,没有任何错误:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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