我的数据有 39 行和 13 列,第一是年,其余是月。在将其转换为时间序列时,我遇到了麻烦。我的数据看起来像:我已经尝试过的原始数据的一部分:
SLP <- as.matrix(SLP_anamoly_series, nrow = 39, ncol = 12)
set.seed(12)
SLP.df <- as.data.frame(SLP)
months <- format(seq.Date(as.Date("2013-01-01"), as.Date("2013-12-01"),
by = "month"), format = "%b")
colnames(SLP.df) <- months
SLP.df$Year <- seq(1979, 2017) # setting as variable and not rowname
#Melting data, so we have dataframe with 39*12 rows
library(reshape2)
SLP.df <- melt(SLP.df, id.vars = "Year")
#ordering the observations by date:
SLP.df$Date <- as.Date(paste(SLP.df$Year, SLP.df$variable, "01", sep = "-"),
format = ("%Y-%b-%d"))
SLP.df <- SLP.df[order(SLP.df$Date), ]
#applying ts() and showing the desired behaviour
SLP.df.ts <- ts(SLP.df$value, start=c(1979,1), end=c(2017,1), frequency=12)
在输出中,数据将转换为ts类,但开始时将粘贴year列,并合并NA值。“NA”被纳入月份属性此外,数据如下移动:[Jan 列将年份作为数据,值转移到 feb 等][3] 请帮忙
您可以在不重塑数据的情况下执行此操作。
SLP.ts <- ts(as.vector(t(SLP)),
start=c(1979,1), end=c(2017,12), frequency=12)
SLP
你在哪里matrix
有 12 列。
带有虚拟数据的演示:
library(dplyr)
df <- data_frame(jan = rnorm(11), feb = rnorm(11), mar = rnorm(11), apr = rnorm(11),
may = rnorm(11), jun = rnorm(11), jul = rnorm(11), aug = rnorm(11),
sep = rnorm(11), oct = rnorm(11), nov = rnorm(11), dec = rnorm(11))
df
# A tibble: 11 × 12
# jan feb mar apr may jun jul aug
# <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 -0.7277503 0.49203901 -1.13683935 0.44493938 0.9787063 -0.5965367 0.29731164 -1.2694916
# 2 2.0578348 0.38561186 -1.51999945 -0.59548193 0.3029656 1.1741675 0.43631169 1.3844318
# 3 1.3249527 0.09741269 -0.31423972 0.47217778 0.7478160 -1.8150642 -0.34418245 -2.6152873
# 4 -1.3849326 -0.42547285 1.30308770 -0.94788520 1.0027448 1.1740929 0.82583414 -1.2775077
# 5 -0.3177837 -1.79757244 1.26156967 -0.18426188 -0.5292503 -0.1482064 0.65681287 -0.4899618
# 6 0.1783239 0.08827373 0.09984112 -0.26299795 -0.4508580 2.0388265 -1.86185049 0.2263487
# 7 -1.7751816 -0.35399751 0.59095293 -0.99107748 0.4541772 -0.3005032 0.41004362 -0.1532398
# 8 1.4083479 0.90696406 1.10822694 2.97771956 0.7045538 -0.9063333 -1.30249178 -0.9727064
# 9 1.1097756 -1.79374219 -0.28359382 2.03726012 0.4181745 1.4613433 3.08177756 -0.9129661
# 10 0.5645198 0.18942828 0.57254031 0.03366888 -0.5167837 0.9518762 1.85161175 -0.1574078
# 11 -0.1483672 -0.31044439 1.05764639 1.03100621 -0.9845712 -0.8245992 -0.05860948 -0.8921633
# ... with 4 more variables: sep <dbl>, oct <dbl>, nov <dbl>, dec <dbl>
SLP <- as.matrix(df)
SLP.ts <- ts(as.vector(t(SLP)),
start=c(1979,1), end=c(1989,12), frequency=12)
SLP.ts
# Jan Feb Mar Apr May Jun Jul Aug
# 1979 -0.72775035 0.49203901 -1.13683935 0.44493938 0.97870627 -0.59653672 0.29731164 -1.26949164
# 1980 2.05783479 0.38561186 -1.51999945 -0.59548193 0.30296562 1.17416747 0.43631169 1.38443180
# 1981 1.32495271 0.09741269 -0.31423972 0.47217778 0.74781602 -1.81506422 -0.34418245 -2.61528729
# 1982 -1.38493258 -0.42547285 1.30308770 -0.94788520 1.00274483 1.17409292 0.82583414 -1.27750774
# 1983 -0.31778370 -1.79757244 1.26156967 -0.18426188 -0.52925034 -0.14820640 0.65681287 -0.48996183
# 1984 0.17832394 0.08827373 0.09984112 -0.26299795 -0.45085804 2.03882653 -1.86185049 0.22634867
# 1985 -1.77518164 -0.35399751 0.59095293 -0.99107748 0.45417718 -0.30050318 0.41004362 -0.15323982
# 1986 1.40834791 0.90696406 1.10822694 2.97771956 0.70455384 -0.90633330 -1.30249178 -0.97270635
# 1987 1.10977565 -1.79374219 -0.28359382 2.03726012 0.41817454 1.46134334 3.08177756 -0.91296614
# 1988 0.56451981 0.18942828 0.57254031 0.03366888 -0.51678368 0.95187622 1.85161175 -0.15740777
# 1989 -0.14836721 -0.31044439 1.05764639 1.03100621 -0.98457119 -0.82459916 -0.05860948 -0.89216334
# Cols Sept-Dec not shown here
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句