R 重塑每日时间序列数据
我有从1980年开始到2013年结束的每日时间序列数据,格式如下。到目前为止,我的代码是R 重塑每日时间序列数据,r,time-series,reshape,R,Time Series,Reshape,我有从1980年开始到2013年结束的每日时间序列数据,格式如下。到目前为止,我的代码是 # trying to reshape my data require(reshape) data <- melt(data1, id.vars=c("year","month")) #尝试重塑我的数据 需要(重塑) 数据我使用了您上传的数据,因此它对我来说如下所示: dat<-read.csv('a.csv') library(reshape) newDF<-res
# trying to reshape my data
require(reshape)
data <- melt(data1, id.vars=c("year","month"))
#尝试重塑我的数据
需要(重塑)
数据我使用了您上传的数据,因此它对我来说如下所示:
dat<-read.csv('a.csv')
library(reshape)
newDF<-reshape(dat,direction='long',idvar=c('year','month'),varying=list(3:33),v.names='X')
newDF<-as.ts(newDF)
dat我使用了您上传的数据,因此它对我的解读如下:
dat<-read.csv('a.csv')
library(reshape)
newDF<-reshape(dat,direction='long',idvar=c('year','month'),varying=list(3:33),v.names='X')
newDF<-as.ts(newDF)
dat与Jason的结果相同,但使用tidyr::gather
而不是重塑
new.df <- gather(dat, key = year, value=month, na.rm = FALSE, convert = TRUE)
new.df$variable <- as.numeric(sub("X", "", new.df$var))
names(new.df)[3] <- "day"
new.df.ts <- as.ts(new.df)
head(new.df.ts)
year month day value
[1,] 1980 1 1 2.3
[2,] 1980 2 1 1.0
[3,] 1980 3 1 0.0
[4,] 1980 4 1 1.8
[5,] 1980 5 1 3.8
[6,] 1980 6 1 10.4
new.df与Jason的结果相同,但使用tidyr::gather
而不是重塑
new.df <- gather(dat, key = year, value=month, na.rm = FALSE, convert = TRUE)
new.df$variable <- as.numeric(sub("X", "", new.df$var))
names(new.df)[3] <- "day"
new.df.ts <- as.ts(new.df)
head(new.df.ts)
year month day value
[1,] 1980 1 1 2.3
[2,] 1980 2 1 1.0
[3,] 1980 3 1 0.0
[4,] 1980 4 1 1.8
[5,] 1980 5 1 3.8
[6,] 1980 6 1 10.4
new.df扩展Jason/Dominic的解决方案这为您提供了一个示例,说明如何按照要求将数据绘制为xts时间序列:
library(xts)
dat<-read.csv('~/Downloads/stack_a.csv')
dat.m <-reshape(dat,direction='long',idvar=c('year','month'),varying=list(3:33),v.names='value')
dat.m <- dat.m[order(dat.m[,1],dat.m[,2],dat.m[,3]),] # order by year, month, day(time)
dat.m$date <-paste0(dat.m$year,'-',dat.m$month,'-',dat.m$time) # concatenate these 3 columns
dat.m <- na.omit(dat.m) # remove the NAs introduced in the original data
dat.xts <- as.xts(dat.m$value,order.by = as.Date(dat.m$date))
names(dat.xts) <- 'value'
plot(dat.xts)
库(xts)
dat扩展Jason/Dominic的解决方案这为您提供了一个示例,说明如何按照要求将数据绘制为xts时间序列:
library(xts)
dat<-read.csv('~/Downloads/stack_a.csv')
dat.m <-reshape(dat,direction='long',idvar=c('year','month'),varying=list(3:33),v.names='value')
dat.m <- dat.m[order(dat.m[,1],dat.m[,2],dat.m[,3]),] # order by year, month, day(time)
dat.m$date <-paste0(dat.m$year,'-',dat.m$month,'-',dat.m$time) # concatenate these 3 columns
dat.m <- na.omit(dat.m) # remove the NAs introduced in the original data
dat.xts <- as.xts(dat.m$value,order.by = as.Date(dat.m$date))
names(dat.xts) <- 'value'
plot(dat.xts)
库(xts)
dat重塑
是一个基本函数。为什么要加载重塑包?非常感谢Jason。这给了我部分的解决办法。它每天都在分组。理想情况下,我希望有一个从1980年1月1日到1980年1月2日的时间序列,然后是2013年12月30日到2013年12月31日的时间序列……这样我就可以绘制序列重塑
是一个基本函数。为什么要加载重塑包?非常感谢Jason。这给了我部分的解决办法。它每天都在分组。理想情况下,我希望有一个1980年1月1日、1980年1月2日、2013年12月30日、2013年12月31日的时间序列……这样我就可以画出序列了非常感谢Dominic。我了解新的软件包!。。。这种方法为我提供了部分解决方案。它每天都在分组。理想情况下,我希望有一个1980年1月1日、1980年1月2日、2013年12月30日、2013年12月31日的时间序列……这样我就可以画出序列了非常感谢Dominic。我了解新的软件包!。。。这种方法为我提供了部分解决方案。它每天都在分组。理想情况下,我希望有一个1980年1月1日、1980年1月2日、2013年12月30日、2013年12月31日的时间序列……这样我就可以画出序列了非常感谢你们!真的很感激!非常感谢大家!真的很感激!