Is it possible to get a matrix column by name from a matrix?
I tried various approaches such as myMatrix["test", ]
but nothing seems to work.
To get a specific column of a matrix, specify the column number preceded by a comma, in square brackets, after the matrix variable name. This expression returns the required row as a vector.
Row & column names using dimnames() in RThe dimnames() command can set or query the row and column names of a matrix. Unlike rownames() or colnames() the dimnames() command operates on both rows and columns at once.
Yes. But place your "test" after the comma if you want the column...
> A <- matrix(sample(1:12,12,T),ncol=4) > rownames(A) <- letters[1:3] > colnames(A) <- letters[11:14] > A[,"l"] a b c 6 10 1
see also help(Extract)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With