Remove or Show NA Values in Table in R (2 Examples)

 

In this R tutorial you’ll learn how to remove or show NA values in a frequency table.

The tutorial will contain the following:

Let’s just jump right in…

 

Example Data

As the first step, let’s create some example data in R:

x <- c("a", "b", "a", NA, "a", "c", "b", NA)    # Create example vector
x                                               # Print example vector
# [1] "a" "b" "a" NA  "a" "c" "b" NA

Have a look at the previous output of the RStudio console. It shows that our example data is a character vector containing letters and some NA values (i.e. missing data).

 

Example 1: Remove NA from Table

In this example, I’ll demonstrate how to create a frequency table without NA values using the table() function in R.

Have a look at the R code below:

table(x)                                        # Table without NA values
# x
# a b c 
# 3 2 1

As you can see, the NA values have automatically been deleted in the previous output.

 

Example 2: Show NA in Table

The following R syntax illustrates how to display NA values in a frequency table.

For this task, we can apply the useNA argument of the table function as shown below:

table(x, useNA = "always")                      # Table with NA values
# x
#    a    b    c  
#    3    2    1    2

The previous output illustrates the count of each character as well as the number of NA values in our vector.

 

Video & Further Resources

I have recently published a video instruction on my YouTube channel, which illustrates the topics of this article. You can find the video below:

 

 

Besides that, you may read the related articles which I have published on my website. I have published several articles about related topics such as missing data, graphics in R, vectors, and ggplot2.

 

In this R tutorial you have learned how to remove or explicitly display NA values in a frequency table. Please let me know in the comments below, if you have any further comments and/or questions.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top