Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding extra column name and row name to a table in R?

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.

like image 284
Arihant Avatar asked Nov 04 '25 00:11

Arihant


1 Answers

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
like image 132
Josh O'Brien Avatar answered Nov 05 '25 14:11

Josh O'Brien



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!