[R] xtabs - missing combination

Patrick Hausmann patrick.hausmann at uni-bremen.de
Sun Dec 13 01:21:48 CET 2009


Dear list,

I am trying to make a contingency table with xtabs but I am getting
a 0 where I expect a 'NA'. Here is a simple example:

options(stringsAsFactors = FALSE)
rn <- LETTERS[1:4]
df1 <- data.frame(r07 = rep(rn, each=4),
                   r08 = rep(rn, 4), value = 1:16)
xtabs(value ~ r07 + r08, df1)

# Delete the combination [A, C]
df1 <- df1[-3,]

# Set 'value' for this combination to 0
df1[13, 3] <- 0

# This is the output I want
tapply(df1[, "value"], df1[, c("r07", "r08")], c)

# but using 'xtabs' I get a 0 for [A, C]
xtabs(value ~ r07 + r08, df1)

Hmm, what have I missed...

Thanks for any help!

Best,
Patrick




More information about the R-help mailing list