In a iterative algorithm, I identify at each step one, several or no row(s) to be taken into account for further calculation. To store the row(s) of interest, I must bind two variables: X.id and X.val. I currently use :
cbind(X.id,X.val)
It works fine when X.id and X.val are both matrices:
X.id <- matrix(1,nrow=2,3)
X.val <- matrix(1,nrow=2,1)
cbind(X.id,X.val)
[,1] [,2] [,3] [,4]
[1,] 1 1 1 1
[2,] 1 1 1 1
but not when they have one row:
X.id <- c(1,1,1)
X.val <- matrix(1,nrow=1,1)
cbind(X.id,X.val)
Which gives the following error:
In cbind(c(1, 1, 1), matrix(1, nrow = 1, 1)) : number of rows of result is not a multiple of vector length (arg 1)
The proposed solution should work when the number of row(s) is(are) 0,1 and n while conserving the dimension of the matrices!