如何在表中包含 NA 数据

gfa*_*001 8 r crosstab xtable na

我最近一直在使用tabR 中的包来构建频率表。使用tabfreq()tabmulti()函数,默认输出不包括 NA 值。有谁知道在这些函数中包含 NA 值的命令?

coi*_*oip 12

table()基础 R 中的函数可以通过 显示缺失值(即 NA)useNA,它带有几个参数:“no”、“ifany”或“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
Run Code Online (Sandbox Code Playgroud)


Mar*_*dri 4

一个可能的解决方案:

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)     |      |
+----------+-----------------+-----------------+-------------------+------+
Run Code Online (Sandbox Code Playgroud)