# Developers Planet

Kemel February 2016

### Create Time series from Matrix

I want to create time series from the matrix where I have the information if the system on(1) or off(0) . So in the m1 I have 5433 hours with the value 20 then from 5433 to 5574 the value is 0 and so on. In the second matrix the value is 40. Now I want to make a time series with the sum of both matrices to have 8760 values.

``````m1 <- matrix(c(5433, 1, 5574, 0,8164,1,8242,0,8760,1), nrow = 5, ncol = 2,   byrow = TRUE)
m2<-matrix(c(1428, 1,7642,0,8760,1), nrow = 3, ncol = 2, byrow = TRUE)

> m1
[,1] [,2]
[1,] 5433    1
[2,] 5574    0
[3,] 8164    1
[4,] 8242    0
[5,] 8760    1
> m2
[,1] [,2]
[1,] 1428    1
[2,] 7642    0
[3,] 8760    1
``````

I can do it with rep function for m1

``````a<-rep(c(20,0,20,0,20),c(5433,5574-5433,8164-5574,8242-8164,8760-8242))

20 20 20 20 20 20
``````

mtoto February 2016

You could transform `1` in your binary columns to `20` and `40` respectively, add a column that defines the `times` in rep, generate vectors then add them up:

``````# transform binary values
m1[m1 == 1] <- 20
m2[m2 == 1] <- 40

m1 <- cbind(m1, c(m1[1,1], diff(m1[,1])))
m2 <- cbind(m2, c(m2[1,1], diff(m2[,1])))

# generate timeseries
a <- rep(m1[,2], m1[,3])
b <- rep(m2[,2], m2[,3])

a + b
``````

#### Post Status

Viewed 2,607 times
Voted 10