Samantha V
Samantha V

Reputation: 31

Display missing values with Crosstable()

I'm trying to display a frequency table using CrossTable().

I want it to display the same results as:

   table(mydata$union, exclude=NULL)

I can't find any documentation on this, but all I have is:

  CrossTable(mydata$union,digits=4)

Upvotes: 3

Views: 1814

Answers (1)

Claudiu Papasteri
Claudiu Papasteri

Reputation: 2609

It seems that the gmodels::CrossTable function does not deal with R type NAs. It seems to be heavily inspired by SPSS and SAS, which have different conventions for NAs. The missing.include argument that keeps or removes any unused factor levels doesn't seem very helpful either.

If you come from a SPSS background, I suggest doing something similar to what you do in SPSS: replacing NAs with a conventional value of 9999. You can run the tidyr::replace_na on your whole dataframe or on specific columns. Here is a small reproducible example:

library(gmodels)
library(tidyr)

data <- mtcars
data$am[c(3, 7, 10)] <- NA                    # replace some values with NAs in am column for example purposes
data$am <- tidyr::replace_na(data$am, 9999)   # replace NAs with 9999 like would be the case in SPSS

gmodels::CrossTable(data$am, digits = 4, format = "SPSS")
#> 
#>    Cell Contents
#> |-------------------------|
#> |                   Count |
#> |             Row Percent |
#> |-------------------------|
#> 
#> Total Observations in Table:  32 
#> 
#>           |        0  |        1  |     9999  | 
#>           |-----------|-----------|-----------|
#>           |       17  |       12  |        3  | 
#>           |  53.1250% |  37.5000% |   9.3750% | 
#>           |-----------|-----------|-----------|
#> 
#> 

Created on 2021-02-06 by the reprex package (v0.3.0)

Upvotes: 2

Related Questions