Simone - 10 months ago 79

R Question

I need to repeatedly add a vector to a matrix. Both take on different lengths everytime I do this. The complete matrix is then used for further analysis (plotting, t-test) Three months ago this code worked:

`mlen <- max(length(matrix), length(vector))`

length(maxtrix) <- length(vector) <- mlen

matrix <- cbind(matrix, vector)

I don't use any specific packages for that. Data input is unchanged a csv file. Now I have either of the following the issues:

a) the unequal length function doesn't work properly anymore. I.e. if the new vector has 970 'rows' but the longest column in the existing matrix has only 270 rows, then the remaining 500 rows of the added vector just get cut off.

The warning message is

`In function (..., deparse.level = 1) :`

number of rows of result is not a multiple of vector length (arg 2)

This doesn't always happen.

b) the values of the vector that is added get placed in empty cells at the bottom of an existing column in the matrix.

Both seriously screws up my further analysis. I have tried to use do.call(cbind...) as suggested here, merge, or append. Nothing procudes the output I need, which is a matrix with 1 column per vector witout any data loss or mixing.

Thanks!

Throwing everyting in a list and the creating a matrix is not useful for me unless the original data structure can be preserved.

Answer

Implemented Tyler's solution here. For completion purposes here is the code again:

```
cbind.fill <- function(...){
nm <- list(...)
nm <- lapply(nm, as.matrix)
n <- max(sapply(nm, nrow))
do.call(cbind, lapply(nm, function (x)
rbind(x, matrix(, n-nrow(x), ncol(x)))))
}
matrix <- cbind.fill(matrix, vector)
```

Using nrow resulted in the new data being written in NA cells of previous columns instead of a new column. For all those interested in the difference between nrow and length

Source (Stackoverflow)