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

I have a matrix that looks like this:

> 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 rows and columns? For instance.

                  thermostat
                  ON OFF
motion_sensor ON   1   1
              OFF  0   1

I looked at ?dimnames, but could not figure / understand how to do this.

+4
source share
1 answer

Try with names. dimnamesis list. In your example, there were no names for elements listthat can be assigned usingnames

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

All Articles