Search code examples
rdataframematrixcolumnsorting

R: how to assign columns to a matrix by column-name


I have an empty matrix with the right shape (cols and rows). I want to assign the columns to this matrix one at a time.

allf <- matrix(0, nrow = 2*h, ncol = ncol(ally))
colnames(allf)<- colnames(train)

Here, allf has columns from AA, AB.... AZ. 

Now, I want to assign the values by column name. As in:

allf.AA <- someothermatrix['xyz']

Solution

  • This will do it.

    allf[,'AA'] <- someothermatrix[,'xyz']