[Rd] xtabs and NA
Milan Bouchet-Valat
nalimilan at club.fr
Sat Feb 14 20:00:19 CET 2015
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
More information about the R-devel
mailing list