nee
nee

Reputation: 125

reshaping daily time series data

I have daily time series data starting from 1980 and ends at 2013 and it is the following format https://www.dropbox.com/s/i6qu6epxzdksvg7/a.xlsx?dl=0. My codes thus far are

   # trying to reshape my data
   require(reshape)
   data <- melt(data1, id.vars=c("year","month"))

However, this did not me my desired output. I would like to have my data in a 4 columns ( year, month, day and data ) or 2 columns with ( date and data) in a time series ( starting from 1st Jan 1980 and ends 31st Dec 2013)

I would be grateful for some guidance on how to get this done.

With kind regards

Upvotes: 1

Views: 863

Answers (3)

hvollmeier
hvollmeier

Reputation: 2986

Extending Jason's / Dominic's solution this gives you an example of how to plot your data as a xts time series as you asked for:

    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)

Upvotes: 2

Dominic Comtois
Dominic Comtois

Reputation: 10401

Same results as Jason's, but using tidyr::gather instead of reshape

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

Upvotes: 2

Jason
Jason

Reputation: 1569

I used the data you uploaded so it read for me like follows:

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)

Is that what you wanted?

Upvotes: 2

Related Questions