You could also use reshape
function from stats
package, but you need to transform your matrix
to data.frame
and form data.frame
to matrix
. But I think that it is more flexible way, because you create id for variable that you want to be responsible for columns (in your case day).
m.df<-as.data.frame(m) ## convert to data.frame
m.df$id<-ave(m.df$day,m.df$day,FUN=seq_along) ### create index with ave function (more solutions: http://stackoverflow.com/questions/8997638/numbering-by-groups )
m2<-reshape(m.df,idvar='id',timevar='day',direction='wide') ## reshape data timevar is responsible for columns and with direction you tell how data set should be expaned.
as.matrix(m2) ### convert back to matrix