Table Including Explicit NA's in R> 3.4.0

EDIT: The accepted answer helped the scale fall out of my eyes; this change is an improvement and not annoying in the end.

The help file for table

now says:

The non-factor arguments a are coerced through the factor (a, exclude = exclude). As of R 3.4.0, excluded values ​​are not considered (where they were included in the NA account, previously).

This is annoying. Before that, you could call table(x, exclude = NULL)

and get explicit confirmation of the number of values NA

. Now if they are not there, you are not told. Note:

vec_with_no_nas <- c("A", "B", "B", "C")
vec_with_nas <- c("A", "B", NA, "C")

table(vec_with_no_nas)
table(vec_with_no_nas, exclude = NULL)

table(vec_with_nas)
table(vec_with_nas, exclude = NULL)

      

This gives the result:

> table(vec_with_no_nas)
vec_with_no_nas
A B C 
1 2 1 
> table(vec_with_no_nas, exclude = NULL)
vec_with_no_nas
A B C 
1 2 1 

      

Cm? there is no explicit confirmation of zero NA.

I really want something like the old behavior that was:

> table(vec_with_no_nas, exclude = NULL)
vec_with_no_nas
A B C <NA>
1 2 1 0

      

FWIW, if the vector has NA values table(x, exclude = NULL)

will tell you:

> table(vec_with_nas)
vec_with_nas
A B C 
1 1 1 

> table(vec_with_nas, exclude = NULL)
vec_with_nas
   A    B    C <NA> 
   1    1    1    1 

      

I work in the database and in tidyverse

. Is there an insert table

that will make explicit confirmation of the absence of NA?

+3


source to share


1 answer


You can try setting the useNA argument to "always". B R 3.2.5

table(vec_with_no_nas, useNA="always")

      

adds the NA column even if there is no NA.

vec_with_no_nas
   A    B    C <NA> 
   1    2    1    0 

      

The help file for 3.4.0 (and 3.2.5) says:



useNA controls if the table contains a count of NA values.

So this argument seems to be directly related to what you want to do. The exclude argument allows the user to directly downgrade the factor variable from the table output.

table(vec_with_no_nas, exclude="A")
vec_with_no_nas
B C 
2 1 

      

What could be cleaner than removing unwanted levels from the constructed table object.

Note:
The online help file 3.4.5 mentions the pathological case of using both exclude and useNA arguments at the same time, and provides an example that might be useful for further investigation.

+4


source







All Articles