3

Let us say I have a matrix,

A = matrix(c(1,23,4,5,6,3,2,2,1,2), nrow = 2, ncol = 5)

with rownames and column names given by,

rownames(A) = c('row1', 'row2')
colnames(A) = c('es', 'one', 'column', 'no', '5')

If I do:

> my.slice = A[1,,drop=FALSE]
    es    one column     no      5 
     1      4      6      2      1 

I still see the column names. But if I do:

> my.slice = as.numeric(A[1,,drop=FALSE])
[1] 1 4 6 2 1

I lost the column names, and both are actually of numeric class.

Is there a way to do as.vector and keep the column names? Or more generally, to slice the matrix into a numeric class vector with names(my.slice) = colnames(A)?

It all started when I set up the default to be drop=FALSE. Then I need to do as.vector when i slice matrices but sadly now I loose the names of the elements in my.slice.

starball
  • 20,030
  • 7
  • 43
  • 238
Dnaiel
  • 7,622
  • 23
  • 67
  • 126
  • `is.vector(A[1,])` returns `TRUE`, primarily because `[` has `drop=TRUE` set by default. Try `dput(A[1,])` you'll see it is just a vector with some names attached. – thelatemail Jul 22 '14 at 04:19
  • @thelatemail thanks. that's a nice answer but I am looking for a short one that keeps me in the numeric class since all my code is built into operations in that class. – Dnaiel Jul 22 '14 at 04:25
  • It **is** numeric - `is.numeric(A[1,])` == `TRUE` – thelatemail Jul 22 '14 at 04:26
  • @thelatemail it's nice. some follow up Qs, can I turn off the printing behavior? and is it as fast as as.vector? I am using as.vector in for loops so having this print the structure to the screen makes it inconvenient. Other than that looks like a good way to go – Dnaiel Jul 22 '14 at 04:29
  • I'm getting confused here. Subsetting one row will give you a vector with names - `as.vector` is not required. – thelatemail Jul 22 '14 at 04:35
  • 2
    Don't do that then. Overriding defaults is a first class way to balls up your code. – thelatemail Jul 22 '14 at 04:37
  • setNames(as.numeric(A[1,,drop=F]),colnames(A)) es one column no 5 1 4 6 2 1 – akrun Jul 22 '14 at 07:37
  • 1
    @Dnaiel: don't change the default, it potentially breaks all packages relying on normal behavior. Don't do it, bro. Especially if our need is only for pretty-printing. Leave the default behavior, write a formatting function instead. – smci Jun 11 '15 at 10:47

2 Answers2

3

You can convert the matrix to a data frame, slice it, and use unlist to make the slice into a named vector:

B <- as.data.frame(A)

my.slice <- unlist(B[1,])

This works as long as the column names do not contain strange characters: the column names of a data frame must be syntactically acceptable as the names of variables, so they can't begin with numbers or punctuation.

Community
  • 1
  • 1
Adrian Baddeley
  • 1,956
  • 1
  • 8
  • 7
3

Using setNames:

(my.slice = setNames(A[1,], colnames(A)))
es    one column     no      5 
 1      4      6      2      1 

class(my.slice)
[1] "numeric"
tflutre
  • 3,354
  • 9
  • 39
  • 53