coding_heart
coding_heart

Reputation: 1295

Reshape Data into Time Series using R

I have a seemingly simple question that I can't figure out. I would like to take a dataset in which each time period has its own variable (i.e. column) for an observation and reformat it such that each observation has just one variable that ranges over time periods. My current data looks like:

obs <- 1:4
y1 <- 5:8
y2 <- 9:12  
data_matrix <- cbind(obs, y1, y2)   

which produces:

     obs y1 y2
[1,]   1  5  9
[2,]   2  6 10
[3,]   3  7 11
[4,]   4  8 12

and I would like it to look like (also creating a time period variable, T):

     obs  T y2
[1,]   1  1  5
[2,]   1  2  9
[3,]   2  1  6
[4,]   2  2 10
[5,]   3  1  7
[6,]   3  2 11
[7,]   4  1  8
[8,]   4  2 12

Thanks for any advice on how to reshape this.

Upvotes: 1

Views: 535

Answers (2)

Docuemada
Docuemada

Reputation: 1779

You can reshape your data:

data_matrix<-data.frame(data_matrix)
reshape(data_matrix,varying=list(2:3),times=names(data_matrix)[2:3],idvar="obs",v.names="value",direction="long")

returns:

     obs time value
1.y1   1   y1     5
2.y1   2   y1     6
3.y1   3   y1     7
4.y1   4   y1     8
1.y2   1   y2     9
2.y2   2   y2    10
3.y2   3   y2    11
4.y2   4   y2    12

You can then sort it by obs.

Upvotes: 1

David
David

Reputation: 9405

The root of your problem is that cbind() is for appending columns and you are looking to combine both rows and columns. There are a lot of different ways to approach this, but if your example is actually this simple (ie: only these few columns) then it's easy to just create two data frames via data.frame() and then combine them via rbind():

> rbind(data.frame(obs,y2=y1,T=1),data.frame(obs,y2,T=2))
  obs y2 T
1   1  5 1
2   2  6 1
3   3  7 1
4   4  8 1
5   1  9 2
6   2 10 2
7   3 11 2
8   4 12 2

Upvotes: 1

Related Questions