How to display NAs while using table() in R?

Problem:
How to display NA records while using table() in R?
Solution:
In R, we can use the Table() function to return the count of observations for each level of a factor variable. Let’s use an example for better understanding.

We will use the Titanic Dataset from Kaggle for this example. We can load the “train.csv” file alone. Le’s have a look at the Data.

As we can see, this “titanic_train” dataset has 891 observations and 12 variables. If we want to find out the levels of the variable “Embarked” and how many observations are there for each of those levels, we can use the Table() function. Let’s have a look at the code in R:

The levels of the factor variable “Embarked” is “C”, “Q” and “S”.

Now, if we want to check the count of records for each level, we will use the table() function.

If we sum up the observations for each level(168+77+644), it will return 889. But, as we have seen earlier, the total observation is 891. That means 2 records are missing.
To resolve this issue, we need to pass exclude = NULL inside the table() function. 

The same results can be achieved by using the Summary() function as below:

Thank You! 

0

Leave a Reply

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