I am new to financial time series, zoo, and xts.

 

I have a length n vector (vDates) of dates. I have an n by k  matrix (mDates). In row i and column j of the matrix, I would like the value to be the row i value of the vector + j months. 

 

mDates[i,j]  = vDates[i] + j months.

 

I would like to do this without loops.

 

I solved the problem with just numbers.

 

v1 = 11:15

m1 = matrix(rep(0,20), nrow = 5)

m1 = v1 + col(m1)

 

I do not know how to add months in zoo or xts, so I tried lubridate.

 

library(lubridate)

vDate = as.Date(c("2011-06-01", "2011-06-02", "2011-06-03"), "%Y-%m-%d")

mDate2  = vDate  + months(1)  this works fine

 

But I do not know how to solve the problem for a matrix.

 

 

library(xts)

charvec = paste("2011-0", 1:2, "-01", sep = "")

theData = matrix(rep(0,6),nrow = 2)

z1 = xts(theData, as.Date(charvec))

 

I want the rows of z1 data to be

2011-02-01  2011-03-01 2011-04-01

2011-03-01  2011-04-01 2011-05-01

 

Thanks.

 

Ira

	[[alternative HTML version deleted]]

