簡體   English   中英

R - data.frame 到 ts 類

[英]R - data.frame to ts class

我有如下容量數據框,我想用它進行 ts 分析。

導入后我的數據是LR_TS如下

  Year    Jan    Feb    Mar    Apr    May    Jun    Jul    Aug    Sep    Oct    Nov    Dec
1 2005 21,608 27,676 29,156 34,632 32,708 29,748 15,836 23,828 36,112 33,448 35,076 31,080
2 2006 36,112 30,488 36,704 28,564 41,884 36,852 20,720 35,076 43,216 42,032 44,696 36,112
3 2007 39,072 33,448 44,104 35,224 50,912 47,368 25,754 40,848 48,856 49,892 48,692 39,368
4 2008 42,180 43,216 40,700 46,340 47,417 45,748 26,656 38,972 46,748 49,248 44,610 37,449
5 2009 39,118 37,342 46,222 41,381 48,839 45,164 27,299 43,782 53,134 54,060 54,746 42,398

月值是因素,現在我嘗試將其更改為 ts 類,但運氣不佳

ts1 <- ts(LR_TS, start = 1, frequency = 1, class = "ts")

與容量值相反,我每個月得到 1 到 5 個。

Year     Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1 2005   1   1   1   2   1   1   1   1   1   1   1   1
2 2006   2   2   2   1   2   2   2   2   2   2   3   2
3 2007   3   3   4   3   5   5   3   4   4   4   4   4
4 2008   5   5   3   5   3   4   4   3   3   3   2   3
5 2009   4   4   5   4   4   3   5   5   5   5   5   5

更新

在我加載數據后我使用

LR_TS[] <- lapply(LR_TS, gsub, pattern=',', replacement='')
setDT(LR_TS)[, names(LR_TS) := lapply(.SD, function(x) if(is.character(x)) as.integer(as.character(x)) else x)]

所以我的 str 現在都是整數。

我使用此代碼將類更改為 ts:

ts1 <- ts(LR_TS, start = 1, frequency = 12, class = "ts")

但結果並不正確。 有任何想法嗎?

由於列有, ,我們需要使用sub刪除它,並將其轉換為numeric

ts(sapply(df1[-1],  function(x) as.numeric(gsub(",", "",x))), start = 1, frequency = 1)
#Time Series:
#Start = 1 
#End = 5 
#Frequency = 1 
#    Jan   Feb   Mar   Apr   May   Jun   Jul   Aug   Sep   Oct   Nov   Dec
#1 21608 27676 29156 34632 32708 29748 15836 23828 36112 33448 35076 31080
#2 36112 30488 36704 28564 41884 36852 20720 35076 43216 42032 44696 36112
#3 39072 33448 44104 35224 50912 47368 25754 40848 48856 49892 48692 39368
#4 42180 43216 40700 46340 47417 45748 26656 38972 46748 49248 44610 37449
#5 39118 37342 46222 41381 48839 45164 27299 43782 53134 54060 54746 42398

factor被強制為其存儲模式值時,OP 帖子中顯示的值是整數存儲值

數據

df1 <- structure(list(Year = 2005:2009, Jan = structure(c(1L, 2L, 3L, 
5L, 4L), .Label = c("21,608", "36,112", "39,072", "39,118", "42,180"
), class = "factor"), Feb = structure(c(1L, 2L, 3L, 5L, 4L), .Label = c("27,676", 
"30,488", "33,448", "37,342", "43,216"), class = "factor"), Mar = structure(c(1L, 
2L, 4L, 3L, 5L), .Label = c("29,156", "36,704", "40,700", "44,104", 
"46,222"), class = "factor"), Apr = structure(c(2L, 1L, 3L, 5L, 
4L), .Label = c("28,564", "34,632", "35,224", "41,381", "46,340"
), class = "factor"), May = structure(c(1L, 2L, 5L, 3L, 4L), .Label = c("32,708", 
"41,884", "47,417", "48,839", "50,912"), class = "factor"), Jun = structure(c(1L, 
2L, 5L, 4L, 3L), .Label = c("29,748", "36,852", "45,164", "45,748", 
"47,368"), class = "factor"), Jul = structure(1:5, .Label = c("15,836", 
"20,720", "25,754", "26,656", "27,299"), class = "factor"), Aug = structure(c(1L, 
2L, 4L, 3L, 5L), .Label = c("23,828", "35,076", "38,972", "40,848", 
"43,782"), class = "factor"), Sep = structure(c(1L, 2L, 4L, 3L, 
5L), .Label = c("36,112", "43,216", "46,748", "48,856", "53,134"
), class = "factor"), Oct = structure(c(1L, 2L, 4L, 3L, 5L), .Label = c("33,448", 
"42,032", "49,248", "49,892", "54,060"), class = "factor"), Nov = structure(c(1L, 
3L, 4L, 2L, 5L), .Label = c("35,076", "44,610", "44,696", "48,692", 
"54,746"), class = "factor"), Dec = structure(c(1L, 2L, 4L, 3L, 
5L), .Label = c("31,080", "36,112", "37,449", "39,368", "42,398"
), class = "factor")), row.names = c("1", "2", "3", "4", "5"), class = "data.frame")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM