首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在将数据更改为时间序列时,会在前几行复制year列

在将数据更改为时间序列时,会在前几行复制year列
EN

Stack Overflow用户
提问于 2017-03-04 08:27:27
回答 1查看 23关注 0票数 0

我的数据有39行和13列,第一列是年份,其余的是月份。在将其转换为时间序列时,我遇到了麻烦。我的数据是这样的:part of original data我已经尝试过了:

代码语言:javascript
复制
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" getting incorporated in the months attribute还,数据被如下移动: Jan列获取年份作为数据,值被转移到feb,等等请帮助

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-04 10:02:38

您可以在不重塑数据的情况下执行此操作。

代码语言:javascript
复制
SLP.ts <- ts(as.vector(t(SLP)), 
                  start=c(1979,1), end=c(2017,12), frequency=12)

其中SLP是有12列的matrix

使用虚拟数据的Demo:

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42590609

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档