Subset Table Object in R (Example)

 

In this post, I’ll illustrate how to subset a table object in R programming.

The article will contain the following:

Let’s take a look at some R codes in action!

 

Creation of Example Data

The following data will be used as basement for this R programming language tutorial:

data <- data.frame(x1 = 1:10,       # Create example data frame
                   x2 = c(rep(letters[1:3], each = 3), "d"))
data                                # Print example data frame

 

table 1 data frame subset table object

 

Have a look at the table that got returned by the previous R syntax. It shows that our example data consists of ten observations and two columns.

Now, we can create a table object based on the column x2 of our data frame:

my_tab <- table(data$x2)            # Create table
my_tab                              # Print table
# a b c d 
# 3 3 3 1

As you can see, the previous R code has created a new table object containing the frequency counts of the variable x2.

You can also see, that we have not subsetted our table yet. Let’s do this!

 

Example: Subset Table Object Using Logical Conditions

In this example, I’ll explain how to extract a subset of a data object with the table class.

We can filter our example table using a logical condition as shown below:

my_tab_sub <- my_tab[my_tab > 1]    # Subset table
my_tab_sub                          # Print subsetted table
# a b c 
# 3 3 3

The previous R code has returned a new table object called my_tab_sub, which contains only table elements with a count larger than 1.

 

Video & Further Resources

I have recently published a video on the Statistics Globe YouTube channel, which explains the R syntax of this tutorial. You can find the video below.

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

In addition, you might have a look at some of the related articles which I have published on this website. A selection of articles is shown below:

 

Summary: You have learned in this tutorial how to select and extract a subset of a table by using a logical condition in R. In case you have any additional comments and/or questions, don’t hesitate to let me know in the comments.

 

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.

Menu
Top