[R] na.action in xtabs - how to include NAs?

Dimitri Liakhovitski dimitri.liakhovitski at gmail.com
Wed Sep 30 16:56:50 CEST 2015


Please help:

# I have a data frame x:
x <- data.frame(a = c(1:3, NA), b = c(NA, 2:4))

# I run a cross-tab keeping NAs, like this:
table(x[c("a", "b")], useNA = "ifany")

# I want to reproduce it using xtabs, but it ignores NAs:
xtabs(~ a + b, x)

# I can't figure out how to force xtabs to include NAs.
# All my attempts below fail to include NAs:
xtabs(~ a + b, x, na.action(na.pass))
xtabs(~ a + b, x, na.action = "na.pass")
xtabs(~ a + b, x, na.action(na.pass(x)))
xtabs(~ a + b, x, exclude = NULL)

Thank you for your hints!
-- 
Dimitri Liakhovitski



More information about the R-help mailing list