1

I have the Capacity data frame as below and I want to use it for ts analysis.

My data is LR_TS as below after import

  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

Months values are factors and now I try to change it to class ts with nil luck

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

I get 1 to 5 for each month as oppose to the Capacity value.

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

UPDATE

After I load the data in I use

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)]

So my str is now all integer.

I use this code to change the class to ts:

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

But the results are not correct. Any ideas?

Kalenji
  • 401
  • 2
  • 19
  • 42

1 Answers1

1

As the columns have ,, we need to remove that with sub, and convert it to 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

The values showedin the OP's post are integer storage values when the factor gets coerced to its storage mode values

data

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")
Community
  • 1
  • 1
akrun
  • 874,273
  • 37
  • 540
  • 662