Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table including explicit NAs in R > 3.4.0

Tags:

r

na

tidyverse

EDIT: The accepted answer has helped the scales fall from my eyes; this change is an improvement and not annoying after all.

In the help file for table, it is now written:

Non-factor arguments a are coerced via factor(a, exclude=exclude). Since R 3.4.0, care is taken not to count the excluded values (where they were included in the NA count, previously).

This is annoying. Before, you could call table(x, exclude = NULL) and get explicit confirmation of the number of NA values. Now, if there are none, you aren't told. Observe:

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 output:

> 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 

See? no explicit confirmation of zero NAs.

What I really want is something like the old behavior, which was:

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

FWIW, if the vector does have 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 base and in the tidyverse. Is there a drop-in table replacement that will do explicit confirmation of no NAs?

like image 313
Alex Coppock Avatar asked Jun 28 '17 12:06

Alex Coppock


People also ask

What does table () do in R?

table() function in R Language is used to create a categorical representation of data with variable name and the frequency in the form of a table.

How do you access table elements in R?

To access the table values, we can use single square brackets. For example, if we have a table called TABLE then the first element of the table can accessed by using TABLE[1].

How do I make a table in R?

We can create a table by using as. table() function, first we create a table using matrix and then assign it to this method to get the table format. Example: In this example, we will create a matrix and assign it to a table in the R language.

How do you call a table in R?

To use table(), simply add in the variables you want to tabulate separated by a comma. Note that table() does not have a data= argument like many other functions do (e.g., ggplot2 functions), so you much reference the variable using dataset$variable.


1 Answers

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

table(vec_with_no_nas, useNA="always")

adds an NA column, even though no NAs are present.

vec_with_no_nas
   A    B    C <NA> 
   1    2    1    0 

The online help file for 3.4.0 (and 3.2.5) says

useNA controls if the table includes counts of NA values.

So this argument seems to do directly address what you want to do. The exclude argument allows the user to directly drop levels of a factor variable from the table output.

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

Which can be cleaner than dropping unwanted levels from a constructed table object.

note:
The online 3.4.0 help file mentions a pathological case in the simultaneous use of both exclude and useNA arguments and also provides an example that might be worth further exploration.

like image 161
lmo Avatar answered Oct 12 '22 16:10

lmo