I have a vector that provides how many \"1\" each row of a matrix has. Now I have to create this matrix out of the vector.
For example, let say I want to create a 4 x 9
One option is sparseMatrix
from Matrix
library(Matrix)
m1 <- sparseMatrix(i = rep(seq_along(v), v), j = sequence(v), x = 1)
m1
#4 x 9 sparse Matrix of class "dgCMatrix"
#[1,] 1 1 . . . . . . .
#[2,] 1 1 1 1 1 1 . . .
#[3,] 1 1 1 . . . . . .
#[4,] 1 1 1 1 1 1 1 1 1
This can be converted to matrix
with as.matrix
as.matrix(m1)