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!
Up-date: Above code lines are part of code doing the following: data import (which vary in size) - data cleaning (data varies even more in size) - storing data in a matrix or dataframe - calculating mean per column, plot / t-test data
Throwing everyting in a list and the creating a matrix is not useful for me unless the original data structure can be preserved.