Chris Snow
Chris Snow

Reputation: 24588

how to set matrix row header name and column header name?

I have a matrix that looks like the following:

> m <- cbind( c(1, 0), c(1, 1) )
> rownames(m) <- c('ON', 'OFF')
> colnames(m) <- c('ON', 'OFF')
> m

    ON OFF
ON   1   1
OFF  0   1

How can I provide a header name for the rows and columns? E.g.

                  thermostat
                  ON OFF
motion_sensor ON   1   1
              OFF  0   1

I had a look at ?dimnames but couldn't see/understand how to do this.

Upvotes: 4

Views: 317

Answers (1)

akrun
akrun

Reputation: 886938

Try with names. dimnames is a list. In your example, there were no names for the list elements, which can be assigned using names

 names(dimnames(m)) <- c('motion_sensor', 'thermostat')
 m
 #            thermostat
 #motion_sensor ON OFF
 #         ON   1   1
 #         OFF  0   1

Upvotes: 7

Related Questions