Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add column and row total in ftable

Tags:

r

I use the ftable to make a table like this:

                HPV-16 negative positive
Sex    HPV-55                           
female negative            2341        4
       positive              11        0
male   negative            2140       23
       positive              25        2

Here is the dput code.

structure(c(2341L, 11L, 2140L, 25L, 4L, 0L, 23L, 2L), .Dim = c(4L, 
2L), class = "ftable", row.vars = list(Sex = c("female", "male"
), `HPV-55` = c("negative", "positive")), col.vars = list(`HPV-16` = c("negative", 
"positive")))

And a sample data of the original data:

structure(list(sex = structure(c(2L, 2L, 1L, 1L, 2L, 1L, 2L, 
2L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L), .Label = c("female", 
"male"), class = c("labelled", "factor"), label = "sex"), orxh16 = structure(c(1L, 
1L, 1L, NA, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L), .Label = c("negative", "positive"), class = c("labelled", 
"factor"), label = "hpv16"), orxh55 = structure(c(1L, 1L, 1L, 
NA, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L), .Label = c("negative", "positive"), class = c("labelled", 
"factor"), label = "hpv55")), row.names = c(NA, -20L), class = c("tbl_df", 
"tbl", "data.frame"))

I tried addmargins: addmargins(tab1, FUN = list(Total=sum), quiet = T), but the detailed information e.g., row names and the layout will be lost.

              Total
      2341  4  2345
        11  0    11
      2140 23  2163
        25  2    27
Total 4517 29  4546

I'm wondering if there is a way to add the column and row total and meanwhile let the layout of the table looks like before (as below)? Thank you!

                HPV-16 negative positive     Total
Sex    HPV-55                           
female negative            2341        4      2345
       positive              11        0       11
male   negative            2140       23      2163
       positive              25        2       27
Total                      4517       29      4546
like image 835
YYM17 Avatar asked Oct 23 '25 18:10

YYM17


2 Answers

addmargins should be used before ftable.

xtab1 <- xtabs(~ sex + orxh55 + orxh16, df)
ftable(addmargins(xtab1, margin = 2:3, list(Total = sum)))

# Margins computed over dimensions
# in the following order:
# 1: orxh55
# 2: orxh16
#                 orxh16 negative positive Total
# sex    orxh55                                 
# female negative              10        0    10
#        positive               0        0     0
#        Total                 10        0    10
# male   negative               9        0     9
#        positive               0        0     0
#        Total                  9        0     9

Sample Data

df <- structure(list(sex = structure(c(2L, 2L, 1L, 1L, 2L, 1L, 2L, 
2L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L), .Label = c("female", 
"male"), class = c("labelled", "factor"), label = "sex"), orxh16 = structure(c(1L, 
1L, 1L, NA, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L), .Label = c("negative", "positive"), class = c("labelled", 
"factor"), label = "hpv16"), orxh55 = structure(c(1L, 1L, 1L, 
NA, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L), .Label = c("negative", "positive"), class = c("labelled", 
"factor"), label = "hpv55")), row.names = c(NA, -20L), class = c("tbl_df", 
"tbl", "data.frame"))
like image 107
Darren Tsai Avatar answered Oct 26 '25 09:10

Darren Tsai


Just found a way to do this, using summarytool::ctable. The tabulation is separated by the grouping variable though, it can provide similar tables as in the ftable.

tab2 <- with(hpv2, stby(list(x=orxh55, y=orxh16), 
    sex, ctable, prop="n", useNA="no", dnn = c("HPV_55", "HPV-16")))
Cross-Tabulation  
HPV_55 * HPV-16  
Data Frame: hpv2  
Group: sex = female  

---------- -------- ---------- ---------- -------
             HPV-16   negative   positive   Total
    HPV_55                                       
  negative                2341          4    2345
  positive                  11          0      11
     Total                2352          4    2356
---------- -------- ---------- ---------- -------

Group: sex = male  

---------- -------- ---------- ---------- -------
             HPV-16   negative   positive   Total
    HPV_55                                       
  negative                2140         23    2163
  positive                  25          2      27
     Total                2165         25    2190
like image 23
YYM17 Avatar answered Oct 26 '25 11:10

YYM17