[英]How to include observations with 'NA' while doing a comparison in data.table?
[英]How to include NA data in a table
我最近一直在使用R中的包tab
來構建頻率表。 使用tabfreq()
或tabmulti()
函數,默認輸出不包括NA值。 有沒有人知道在這些函數中包含NA值的命令?
基數R中的table()
函數可以通過useNA
顯示缺失值(即NA),這需要幾個參數:“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
可能的解決方案:
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) | |
+----------+-----------------+-----------------+-------------------+------+
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.