15

I've recently been using the package tab in R to build frequency tables. Using the tabfreq() or the tabmulti() functions, the default output excludes NA values. Does anyone know the command to include NA values within these functions?

coip
  • 1,312
  • 16
  • 30
gfa2001
  • 227
  • 1
  • 2
  • 10
  • I was able to use @Marco Sandri answer for tabfreq(). However, it does not work for tabmulti(). Any thoughts on how to include NA in the output of a table generated with tabmulti()? – gfa2001 Jul 15 '18 at 19:15

2 Answers2

28

The table() function in base R can display missing values (i.e. NAs) via useNA, which takes several arguments: "no", "ifany", or "always".

data(airquality) # loads the built-in data frame, which has NAs
table(airquality$Ozone, useNA = "always") # always displays the number of missing values
table(airquality$Wind, useNA = "ifany") # only displays the number of missing values if there are some
coip
  • 1,312
  • 16
  • 30
2

A possible solution:

library(tab)
library(Hmisc)
data(d)

# NA was treated as a third level
Sex <- factor(d$Sex, exclude=NULL)
freqtable2 <- tabfreq(x = d$Group, y = Sex)
print.char.matrix(freqtable2, col.names=T)

+----------+-----------------+-----------------+-------------------+------+
| Variable |Overall (n = 300)|Control (n = 136)|Treatment (n = 164)|   P  |
+----------+-----------------+-----------------+-------------------+------+
|Sex, n (%)|                 |                 |                   |<0.001|
+----------+-----------------+-----------------+-------------------+------+
|    Female|    155 (51.7)   |    93 (68.4)    |     62 (37.8)     |      |
+----------+-----------------+-----------------+-------------------+------+
|      Male|    142 (47.3)   |    43 (31.6)    |     99 (60.4)     |      |
+----------+-----------------+-----------------+-------------------+------+
|        NA|       3 (1.0)   |      0 (0.0)    |       3 (1.8)     |      |
+----------+-----------------+-----------------+-------------------+------+
Marco Sandri
  • 23,289
  • 7
  • 54
  • 58
  • Thanks for this, it's quite helpful. Is there a similar way to apply this to numeric columns? – gfa2001 Jul 15 '18 at 17:35
  • @gfa2001 Contingency tables are normally used with categorical variables. I do not understand what do you need... please, explain me your idea. In the meantime, if you found useful my answer, please, upvote and accept it. Thank you. – Marco Sandri Jul 15 '18 at 21:19