r - Create Time series from Matrix -


i want create time series matrix have information if system on(1) or off(0) . in m1 have 5433 hours value 20 5433 5574 value 0 , on. in second matrix value 40. want make time series sum of both matrices 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 rep function m1

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

you transform 1 in binary columns 20 , 40 respectively, add column defines times in rep, generate vectors add them up:

# transform binary values m1[m1 == 1] <- 20 m2[m2 == 1] <- 40  # add column rep() m1 <- cbind(m1, c(m1[1,1], diff(m1[,1]))) m2 <- cbind(m2, c(m2[1,1], diff(m2[,1])))  # generate timeseries <- rep(m1[,2], m1[,3]) b <- rep(m2[,2], m2[,3])  # add them + b 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -