Meaning of $ Operator in R (2 Examples)


In this tutorial you’ll learn how to use the $ operator in the R programming language.

The content of the page is structured as follows:

Let’s start right away!


Example 1: Using $ Operator to Access Data Frame Column

Generally speaking, the $ operator is used to extract or subset a specific part of a data object in R. For instance, this can be a data frame object or a list.

In this example, I’ll explain how to extract the values in a data frame columns using the $ operator.

In preparation for the example, we have to create a data frame in R. We can do that by executing the following R syntax:

data <- data.frame(x1 = 1:5,    # Create example data
                   x2 = letters[1:5],
                   x3 = 9)
data                            # Print example data


table 1 data frame meaning dollar operator r


As shown in Table 1, we have created a data frame with five rows and three columns with the previous R syntax. The names of our variables are x1, x2, and x3.

Let’s assume that we want to extract the second column of our data frame, i.e. the variable x2. Then, we can use the $ operator as shown below:

data$x2                         # Extract column of data
# [1] "a" "b" "c" "d" "e"

Have a look at the previous output of the RStudio console. As you can see, we have extracted only the values stored in the variable x2.


Example 2: Using $ Operator to Access Element of List

As already mentioned, it is also possible to use the $ operator to return specific list elements. To illustrate that, we first have to create an example list in R:

my_list <- list(A = 1:5,        # Create example list
                B = letters[1:5],
                C = 9)
my_list                         # Print example list
# $A
# [1] 1 2 3 4 5
# $B
# [1] "a" "b" "c" "d" "e"
# $C
# [1] 9

The previous output of the RStudio console shows the structure of our list. Our list consists of three list elements that are called A, B, and C. Each of these list elements contains different values.

Let’s assume that we want to return only the values of the list element B. In this case, we can use the $ operator as shown below:

my_list$B                       # Extract element of list
# [1] "a" "b" "c" "d" "e"

The previous output of the RStudio console shows only the values that are contained in the second list element B.


Video, Further Resources & Summary

In case you need further information on the R programming codes of this tutorial, you could watch the following video of the Statistics Globe YouTube channel. In the video, I’m explaining the contents of this article:


The YouTube video will be added soon.


Furthermore, you may want to have a look at the related articles which I have published on this website.


In summary: In this tutorial, I have illustrated how to apply the $ operator in R. Don’t hesitate to let me know in the comments section below, if you have additional 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.