Question

I have a table which I generated from table() function and I further use xtable to print it as follows:

          FF NF NN Sum
     FF   8  0  0   8
     NF   7  8  0  15
     NN   3  1  4   8

I want to add an additional column name and a rowname in the following format.

           Time2
   Time1 FF NF NN Sum
     FF   8  0  0   8
     NF   7  8  0  15
     NN   3  1  4   8

I looked into xtable but couldn't find anything. colnames() changes the names of the existing columns, rownames() does the same to the rownames.

Was it helpful?

Solution

You've got a couple of options.

The first is to add those names to the table object "by hand".

## An example of a table object with unnamed dimnames 
x <- with(warpbreaks, table(unname(wool), unname(tension)))
x
#     L M H
#   A 9 9 9
#   B 9 9 9

names(dimnames(x)) <- c("Time1", "Time2")
x
#      Time2
# Time1 L M H
#     A 9 9 9
#     B 9 9 9

The second (and typically preferable) option is to supply the names in your initial call to table(), like this:

table(Time1 = warpbreaks[[2]], Time2 = warpbreaks[[3]])
#      Time2
# Time1 L M H
#     A 9 9 9
#     B 9 9 9
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top