Meaning of $ Operator in R Explained (2 Examples) | How to Use $ Sign (2024)

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:

1) Example 1: Using $ Operator to Access Data Frame Column

2) Example 2: Using $ Operator to Access Element of List

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

Meaning of $ Operator in R Explained (2 Examples) | How to Use $ Sign (1)

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:

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

  • Assignment Operators in R
  • R Error: $-Operator is Invalid for Atomic Vectors
  • Introduction to R

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.

6 Comments. Leave new

  • Meaning of $ Operator in R Explained (2 Examples) | How to Use $ Sign (2)

    Nadira

    December 22, 2021 12:33 pm

    Hi Joachim,

    Lovely seeing your blog! I have a question specifically for the second example. Do you think it’s possible to use the $ operator to select multiple list elements? So for example, instead of my_list$B only, you include A and B

    Thanks,
    Nadira

    Reply
    • Meaning of $ Operator in R Explained (2 Examples) | How to Use $ Sign (3)

      Joachim

      December 22, 2021 2:02 pm

      Hey Nadira,

      Thanks a lot for the kind words, glad you like my website!

      Regarding your question: I would use the following R code to extract multiple list elements:

      my_list[c("A", "B")]

      Regards,
      Joachim

      Reply
  • Meaning of $ Operator in R Explained (2 Examples) | How to Use $ Sign (4)

    skyrock

    October 3, 2022 10:17 am

    thank you bro, it was so helpfull

    Reply
    • Meaning of $ Operator in R Explained (2 Examples) | How to Use $ Sign (5)

      Joachim

      October 5, 2022 8:51 am

      Hey,

      This is great to hear, glad it helped!

      Regards,
      Joachim

      Reply
  • Meaning of $ Operator in R Explained (2 Examples) | How to Use $ Sign (6)

    soha

    January 5, 2023 10:58 pm

    can i use $ to combine variables of unequal length to a data?

    i have 3 variables of different lengths
    and they are factors
    length(x) = 2 (factor with 2 levels)
    length(y)=2 (factor with 2 levels)
    lenght(f)=5 ( factor of 5 levels and 4 values)
    and the data which is file (p.txt) contain 4403
    every time i try to add this variable as columns to the data it give me error of replacment due to the different length

    how to combine them to the data file without losing their type as factors

    i tried maxlength but it convert them to integer
    i tried $ but it give me NULL the coloumns not added
    i tried cbind but it give me error of different lengths
    what should i do

    Reply
    • Meaning of $ Operator in R Explained (2 Examples) | How to Use $ Sign (7)

      Cansu (Statistics Globe)

      January 9, 2023 10:07 am

      Hello Soha,

      You can check our tutorial: Create Data Frame of Unequal Lengths in R. Let me know if the solution worked for you.

      Regards,
      Cansu

      Reply

Leave a Reply

I’m Joachim Schork. On this website, I provide statistics tutorials as well as code in Python and R programming.

Statistics Globe Newsletter

Related Tutorials

Clean Up Memory in R (Example) | Garbage Collection with gc() Function

Meaning of $ Operator in R Explained (2 Examples) | How to Use $ Sign (2024)

References

Top Articles
Forager How-to Get Archaeology Items - Dino Egg, Anchor, Fossil, Frozen Relic, Frozen Squid, Kapala, Lava Eel, and More!
Marine Area Rules & Definitions - Washington Fishing
Canya 7 Drawer Dresser
Unblocked Games Premium Worlds Hardest Game
Wizard Build Season 28
Online Reading Resources for Students & Teachers | Raz-Kids
Triumph Speed Twin 2025 e Speed Twin RS, nelle concessionarie da gennaio 2025 - News - Moto.it
라이키 유출
Roblox Developers’ Journal
Bloxburg Image Ids
Wfin Local News
Mlifeinsider Okta
Otr Cross Reference
Bros Movie Wiki
Morocco Forum Tripadvisor
Hartland Liquidation Oconomowoc
Bowie Tx Craigslist
Suffix With Pent Crossword Clue
Bend Pets Craigslist
Log in or sign up to view
Nhl Wikia
Army Oubs
Richland Ecampus
Craigslist List Albuquerque: Your Ultimate Guide to Buying, Selling, and Finding Everything - First Republic Craigslist
Promiseb Discontinued
Jenna Ortega’s Height, Age, Net Worth & Biography
Bjerrum difference plots - Big Chemical Encyclopedia
Certain Red Dye Nyt Crossword
Boise Craigslist Cars And Trucks - By Owner
University Of Michigan Paging System
Dashboard Unt
Marokko houdt honderden mensen tegen die illegaal grens met Spaanse stad Ceuta wilden oversteken
Ticket To Paradise Showtimes Near Cinemark Mall Del Norte
Mynahealthcare Login
Craigslist Fort Smith Ar Personals
The Procurement Acronyms And Abbreviations That You Need To Know Short Forms Used In Procurement
Craftsman Yt3000 Oil Capacity
Dairy Queen Lobby Hours
Delta Rastrear Vuelo
Ripsi Terzian Instagram
Ixlggusd
2008 Chevrolet Corvette for sale - Houston, TX - craigslist
Stanley Steemer Johnson City Tn
Colorado Parks And Wildlife Reissue List
Lovely Nails Prices (2024) – Salon Rates
Www Ventusky
Where and How to Watch Sound of Freedom | Angel Studios
Joe Bartosik Ms
Subdomain Finer
683 Job Calls
Lorcin 380 10 Round Clip
Mazda 3 Depreciation
Latest Posts
Article information

Author: Dong Thiel

Last Updated:

Views: 6237

Rating: 4.9 / 5 (59 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Dong Thiel

Birthday: 2001-07-14

Address: 2865 Kasha Unions, West Corrinne, AK 05708-1071

Phone: +3512198379449

Job: Design Planner

Hobby: Graffiti, Foreign language learning, Gambling, Metalworking, Rowing, Sculling, Sewing

Introduction: My name is Dong Thiel, I am a brainy, happy, tasty, lively, splendid, talented, cooperative person who loves writing and wants to share my knowledge and understanding with you.