[R] xtabs ignores l.h.s. rows with NA in just one column - bug or facility?
Bendix Carstensen
bend|x@c@r@ten@en @end|ng |rom reg|onh@dk
Thu Apr 25 13:59:26 CEST 2019
Here is an example showing that xtabs and cbind are not commutative, which at least I thought reading the help page for xtabs.
print( sessionInfo(), l=F )
xx <- c( 6,11,38,17)
yy <- c(NA,26,18,48)
ff <- c('a','a','b','b')
data.frame( xx, yy, ff )
xtabs( cbind(xx,yy) ~ ff )
cbind( xtabs(xx ~ ff ),
xtabs(yy ~ ff ) )
Here is the result from my xomputer:
> print( sessionInfo(), l=F )
R version 3.5.3 (2019-03-11)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 14.04.6 LTS
Matrix products: default
BLAS: /usr/lib/openblas-base/libopenblas.so.0
LAPACK: /usr/lib/lapack/liblapack.so.3.0
attached base packages:
[1] utils datasets graphics grDevices stats methods base
loaded via a namespace (and not attached):
[1] compiler_3.5.3
> xx <- c( 6,11,38,17)
> yy <- c(NA,26,18,48)
> ff <- c('a','a','b','b')
> data.frame( xx, yy, ff )
xx yy ff
1 6 NA a
2 11 26 a
3 38 18 b
4 17 48 b
> xtabs( cbind(xx,yy) ~ ff )
ff xx yy
a 11 26
b 55 66
> cbind( xtabs(xx ~ ff ),
+ xtabs(yy ~ ff ) )
[,1] [,2]
a 17 26
b 55 66
>
I would have guessed the upper left entry would be 17 in the first instance too, but it appears that xtabs does something like complete.cases() on the l.h.s. before summing. At least this should be in the description.
Am I missing something here?
Bendix Carstensen
Steno Diabetes Center Copenhagen
________________________________
Denne e-mail indeholder fortrolig information. Hvis du ikke er den rette modtager af denne e-mail eller hvis du modtager den ved en fejltagelse, beder vi dig venligst informere afsender om fejlen ved at bruge svarfunktionen. Samtidig bedes du slette e-mailen med det samme uden at videresende eller kopiere den.
More information about the R-help
mailing list