r - Adding a new column to matrix error -
i'm trying add new column existing matrix, getting warning everytime.
i'm trying code:
normdismatrix$newcolumn <- labels
getting message:
warning message: in normdismatrix$newcolumn <- labels : coercing lhs list
after it, when check matrix, seems null:
dim(normdismatrix) null
note: labels vectors have numbers between 1 , 4.
what can problem?
as @thelatemail pointed out, $
operator cannot used subset matrix. because matrix single vector dimension attribute. when used $
try add new column, r converted matrix lowest structure $
can used on vector, list.
the function want cbind()
(column bind). suppose have matrix m
(m <- matrix(51:70, 4)) # [,1] [,2] [,3] [,4] [,5] # [1,] 51 55 59 63 67 # [2,] 52 56 60 64 68 # [3,] 53 57 61 65 69 # [4,] 54 58 62 66 70
to add new column vector called labels
, can do
labels <- 1:4 cbind(m, newcolumn = labels) # newcolumn # [1,] 51 55 59 63 67 1 # [2,] 52 56 60 64 68 2 # [3,] 53 57 61 65 69 3 # [4,] 54 58 62 66 70 4
Comments
Post a Comment