Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

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.

like image 349
Chris Snow Avatar asked Mar 16 '23 15:03

Chris Snow


1 Answers

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
like image 156
akrun Avatar answered Mar 18 '23 15:03

akrun