Hi I haven't found a way to produce a tabulation from factor data with NA values using xtabs. Please find a minimal example below, it's also on R-pubs [1]. Tested with R 3.1.2 and R-devel r67720. It doesn't seem to be documented explicitly that it's not supported. From reading the code [2] it looks like the relevant call to table() doesn't set the "useNA" parameter, which I think is necessary to make NAs show up in the result. Am I missing anything? If this a bug -- would a patch be welcome? Do we need compatibility with the current behavior? I'm aware of workarounds, I just prefer xtabs() over table() for its interface. Thanks. Best regards Kirill [1] http://rpubs.com/krlmlr/xtabs-NA [2] https://github.com/wch/r-source/blob/780021752eb83a71e2198019acf069ba8741103b/src/library/stats/R/xtabs.R#L60 data <- factor(letters[1:4], levels = letters[1:3]) data ## [1] a b c <NA> ## Levels: a b c xtabs(~data) ## data ## a b c ## 1 1 1 xtabs(~data, na.action = na.pass) ## data ## a b c ## 1 1 1 xtabs(~data, na.action = na.pass, exclude = numeric()) ## data ## a b c ## 1 1 1 xtabs(~data, na.action = na.pass, exclude = NULL) ## data ## a b c ## 1 1 1 sessionInfo() ## R version 3.1.2 (2014-10-31) ## Platform: x86_64-pc-linux-gnu (64-bit) ## ## locale: ## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C ## [3] LC_TIME=de_CH.UTF-8 LC_COLLATE=en_US.UTF-8 ## [5] LC_MONETARY=de_CH.UTF-8 LC_MESSAGES=en_US.UTF-8 ## [7] LC_PAPER=de_CH.UTF-8 LC_NAME=C ## [9] LC_ADDRESS=C LC_TELEPHONE=C ## [11] LC_MEASUREMENT=de_CH.UTF-8 LC_IDENTIFICATION=C ## ## attached base packages: ## [1] stats graphics grDevices utils datasets methods base ## ## other attached packages: ## [1] magrittr_1.5 ProjectTemplate_0.6-1.0 ## ## loaded via a namespace (and not attached): ## [1] digest_0.6.8 evaluate_0.5.7 formatR_1.0.3 htmltools_0.2.6 ## [5] knitr_1.9.2 rmarkdown_0.5.1 stringr_0.6.2 tools_3.1.2 ## [9] ulimit_0.0-2
On Mon, Feb 9, 2015 at 8:52 AM, Kirill M?ller <kirill.mueller at ivt.baug.ethz.ch> wrote:> Hi > > > I haven't found a way to produce a tabulation from factor data with NA > values using xtabs. Please find a minimal example below, it's also on R-pubs > [1]. Tested with R 3.1.2 and R-devel r67720. > > It doesn't seem to be documented explicitly that it's not supported. From > reading the code [2] it looks like the relevant call to table() doesn't set > the "useNA" parameter, which I think is necessary to make NAs show up in the > result. > > Am I missing anything? If this a bug -- would a patch be welcome? Do we need > compatibility with the current behavior? > > I'm aware of workarounds, I just prefer xtabs() over table() for its > interface. >Passing table the output of model.frame would still allow the use of a formula interface:> mf <- model.frame( ~ data, na.action = na.pass) > do.call("table", c(mf, useNA = "ifany"))a b c <NA> 1 1 1 1 -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
On 09.02.2015 16:59, Gabor Grothendieck wrote:> On Mon, Feb 9, 2015 at 8:52 AM, Kirill M?ller > <kirill.mueller at ivt.baug.ethz.ch> wrote: > Passing table the output of model.frame would still allow the use of a > formula interface: >> mf <- model.frame( ~ data, na.action = na.pass) >> do.call("table", c(mf, useNA = "ifany")) > a b c <NA> > 1 1 1 1 > >Fair enough, this qualifies as a workaround, and IMO this is how xtabs should handle it internally to allow writing xtabs(~data, na.action = na.pass) -- or at least xtabs(~data, na.action = na.pass, exclude = NULL) if backward compatibility is desired. Would anyone with write access to R's SVN repo care enough about this situation to review a patch? Thanks. -Kirill
Le lundi 09 f?vrier 2015 ? 14:52 +0100, Kirill M?ller a ?crit :> Hi > > > I haven't found a way to produce a tabulation from factor data with NA > values using xtabs. Please find a minimal example below, it's also on > R-pubs [1]. Tested with R 3.1.2 and R-devel r67720. > > It doesn't seem to be documented explicitly that it's not supported. > From reading the code [2] it looks like the relevant call to table() > doesn't set the "useNA" parameter, which I think is necessary to make > NAs show up in the result. > > Am I missing anything? If this a bug -- would a patch be welcome? Do we > need compatibility with the current behavior? > > I'm aware of workarounds, I just prefer xtabs() over table() for its > interface. > > Thanks.The best workaround (but still using xtabs()) I have found up to now is to surround variables with addNA(). But I agree this situation is very painful, especially since the docs do not say anything about how to get the NAs to appear. Regards> Best regards > > Kirill > > > [1] http://rpubs.com/krlmlr/xtabs-NA > [2] > https://github.com/wch/r-source/blob/780021752eb83a71e2198019acf069ba8741103b/src/library/stats/R/xtabs.R#L60 > > > data <- factor(letters[1:4], levels = letters[1:3]) > data > ## [1] a b c <NA> > ## Levels: a b c > xtabs(~data) > ## data > ## a b c > ## 1 1 1 > xtabs(~data, na.action = na.pass) > ## data > ## a b c > ## 1 1 1 > xtabs(~data, na.action = na.pass, exclude = numeric()) > ## data > ## a b c > ## 1 1 1 > xtabs(~data, na.action = na.pass, exclude = NULL) > ## data > ## a b c > ## 1 1 1 > sessionInfo() > ## R version 3.1.2 (2014-10-31) > ## Platform: x86_64-pc-linux-gnu (64-bit) > ## > ## locale: > ## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C > ## [3] LC_TIME=de_CH.UTF-8 LC_COLLATE=en_US.UTF-8 > ## [5] LC_MONETARY=de_CH.UTF-8 LC_MESSAGES=en_US.UTF-8 > ## [7] LC_PAPER=de_CH.UTF-8 LC_NAME=C > ## [9] LC_ADDRESS=C LC_TELEPHONE=C > ## [11] LC_MEASUREMENT=de_CH.UTF-8 LC_IDENTIFICATION=C > ## > ## attached base packages: > ## [1] stats graphics grDevices utils datasets methods base > ## > ## other attached packages: > ## [1] magrittr_1.5 ProjectTemplate_0.6-1.0 > ## > ## loaded via a namespace (and not attached): > ## [1] digest_0.6.8 evaluate_0.5.7 formatR_1.0.3 htmltools_0.2.6 > ## [5] knitr_1.9.2 rmarkdown_0.5.1 stringr_0.6.2 tools_3.1.2 > ## [9] ulimit_0.0-2 > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel