[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 16:01:04 CEST 2019


Thanks for that, but the documentation also says that teh default behaviour is to use na.action=na.pass, which normally just ignores missing values, whereas xtabs seems to ignore the entire row...
/Bendix

________________________________________
From: David L Carlson <dcarlson using tamu.edu>
Sent: 25 April 2019 15:55
To: Bendix Carstensen; r-help using r-project.org
Subject: RE: xtabs ignores l.h.s. rows with NA in just one column - bug or facility?

The documentation describes how to control the behavior of missing values:

> xtabs( cbind(xx,yy) ~ ff, addNA=TRUE)

ff  xx yy
  a 17
  b 55 66

But of course, now you do not get 26 in the (a, yy) cell because 26 + NA = NA.

----------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77843-4352

-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Bendix Carstensen
Sent: Thursday, April 25, 2019 6:59 AM
To: r-help using r-project.org
Subject: [R] xtabs ignores l.h.s. rows with NA in just one column - bug or facility?

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.

______________________________________________
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

________________________________


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