Unable to reset R rownames, rownames(df) returns NULL
I am using the ukcars data set in the expsmooth package. I am trying to execute the following code:
decomp <- stl(ukcars, s.window="periodic")
lastyear <- rep(decomp$time.series[110:113,"seasonal"],2)
fc <- fit$mean + lastyear
plot(ukcars,xlim=c(1980,2007))
but I get the error
lastyear <- rep(decomp$time.series[110:113,"seasonal"],2)
Error in `[.default`(decomp$time.series, 110:113, "seasonal") :
subscript out of bounds
This was easy to find out why this is happening, the row index isn't numeric.
df = decomp$time.series
df
seasonal trend remainder
1997 Q1 29.461732 409.5449 -2.7816187
1997 Q2 12.481745 418.2274 10.7778183
1997 Q3 -38.894178 427.0137 -18.5534968
1997 Q4 -3.049312 434.5440 19.2282932
I should be able to fix this taking the current row index and setting it to a new column in the df and resetting the rownames. But I am unable to do this. rownames(df)
returns NULL and when doing rownames(df) = 1:nrow(df)
I get
Error in `[.default`(df, 110:113, "seasonal") : subscript out of bounds
I also tried ncol(df)
but that returns 3
EDIT: including dput(df)
dput(df)
structure(c(29.4617321114283, 12.481744937995, -38.8941781754736,
-3.04931217790218, 29.4617321114283, 12.481744937995, -38.8941781754736,
-3.04931217790218, 29.4617321114283, 12.481744937995, -38.8941781754736,
-3.04931217790218, 29.4617321114283, 12.481744937995, -38.8941781754736,
-3.04931217790218, 29.4617321114283, 12.481744937995, -38.8941781754736,
-3.04931217790218, 29.4617321114283, 12.481744937995, -38.8941781754736,
-3.04931217790218, 29.4617321114283, 12.481744937995, -38.8941781754736,
-3.04931217790218, 29.4617321114283, 12.481744937995, -38.8941781754736,
-3.04931217790218, 29.4617321114283, 409.544886542326, 418.227436805885,
427.01367497542, 434.544018928925, 442.651085971855, 442.447639001895,
437.267648318706, 432.966177328059, 431.539698434268, 439.341038640529,
450.91900577231, 455.122749175224, 444.587943439341, 421.3536533291,
395.452137518301, 375.326317915304, 367.770200274677, 369.335086887743,
380.278749855951, 393.715056976157, 403.779846429226, 409.053577715943,
404.884341815207, 404.446280975672, 407.109838578845, 412.446556196033,
418.085913244093, 418.859041902176, 416.45346591907, 414.103454261138,
409.700294883422, 405.663143500826, 401.068578490248, -2.78161865375392,
10.7778182561204, -18.5534967999465, 19.2282932489768, -9.67081808328351,
13.3026160601095, 5.26252985676808, -15.9688651501567, -0.505430545696072,
-2.89078357852424, -4.23782759683684, 17.3345630026784, 20.2613244492306,
-0.595398267095391, -21.4519593428274, 6.51799426259851, -10.1319323861056,
-9.42183182573785, -5.59457168047692, 6.41425520174482, 16.5134214593455,
-19.2833226539381, 25.8568363602669, -15.5069687977694, -12.2465706902731,
8.35169886597168, 12.0212649313809, -7.06972972427383, -0.45719803049866,
1.61680080086745, 8.24188329205163, -8.57183132292397, 2.26568939832384
), .Dim = c(33L, 3L), .Dimnames = list(c("1", "2", "3", "4",
"5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15",
"16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26",
"27", "28", "29", "30", "31", "32", "33"), c("seasonal", "trend",
"remainder")), .Tsp = c(1997, 2005, 4), class = c("mts", "ts",
"matrix"))
链接地址: http://www.djcxy.com/p/96266.html