There are four forms of the extract operator in R: [, [[, $, and @.
The fourth form is the slot operator and is used to extract content from objects built with the S4 object system, also known as a formally defined object in R.
Most beginning R users don’t work with formally defined objects, so we won’t discuss the slot operator here.
The first form, [, can extract content from vectors, lists, or data frames.
The second and third forms, [[ and $, extract content from a single object.
The $ operator uses a name to perform the extraction as in RObject$Name. Therefore it enables one to extract items from a list based on their names.
What is the difference between [[]] and $ in R
The double brackets([[]]) enable us to select multiple columns, whereas the $ operator only allows us to select one column.
The $ operator has higher precedence than the [[ ]] operator, so if you want to use both operators together, you need to use parentheses to specify the order of operations.
$ sign in R
The $ sign in R is used to access the elements of a list or a data frame by their names. The $ sign can select a variable/column, assign new values to a variable/column, or add a new variable/column in an R object.
Using $ in R on a List
To create a list in R, use the list() function.
listA <- list("Ratchet", "Clank", "Ellie", "Joel")
print(listA)
Output
[[1]]
[1] "Ratchet"
[[2]]
[1] "Clank"
[[3]]
[1] "Ellie"
[[4]]
[1] "Joel"
Use a $ sign to add a new variable to a list
To add a new variable to the R list, use the $(dollar) operator.
listA <- list("PS5" = "Miles Morales", "Xbox" = "Halo Infinite")
print(listA)
print("After adding a variable to a list")
listA$Nintendo <- "Legend of Zelda"
print(listA)
You can see that we used the name of the list, then the $ operator, and the assignment (“<-”) operator.
Output
$PS5
[1] "Miles Morales"
$Xbox
[1] "Halo Infinite"
[1] "After adding a variable to a list"
$PS5
[1] "Miles Morales"
$Xbox
[1] "Halo Infinite"
$Nintendo
[1] "Legend of Zelda"
You can see that we have added Nintendo property with its value, Legend of Zelda to the list using the $ sign in R.
Use the $ sign to select a variable in a list.
We can extract or select a variable from the list using the $ sign.
listA <- list("PS5" = "Miles MorXales", "Xbox" = "Halo Infinite", "Nintendo" = "Legend of Zelda")
print(listA$Nintendo)
Output
[1] "Legend of Zelda"
In this example, we have selected the $Nintendo variable, which gives its value. If you want to select two, or more columns, you must use the double brackets and put each column name as a character.
How to Use a dollar sign in R DataFrame
To add a new column in a data frame, use the $ operator. To select a column in a data frame, you can use the $ sign.
To create a data frame in R, use the data.frame() function.
streaming <- data.frame(
service_id = c(1:5),
service_name = c("Netflix", "Disney+", "HBOMAX", "Hulu", "Peacock"),
service_price = c(18, 10, 15, 7, 12),
stringsAsFactors = FALSE
)
# Print the data frame.
print(streaming)
Output
service_id service_name service_price
1 1 Netflix 18
2 2 Disney+ 10
3 3 HBOMAX 15
4 4 Hulu 7
5 5 Peacock 12
Let’s add a new column in the R data frame using the $ sign.
streaming <- data.frame(
service_id = c(1:5),
service_name = c("Netflix", "Disney+", "HBOMAX", "Hulu", "Peacock"),
service_price = c(18, 10, 15, 7, 12),
stringsAsFactors = FALSE
)
streaming$shows <- c("Stranger Things", "Loki", "Friends", "Castle Rock", "The Office")
print(streaming)
Output
service_id service_name service_price shows
1 1 Netflix 18 Stranger Things
2 2 Disney+ 10 Loki
3 3 HBOMAX 15 Friends
4 4 Hulu 7 Castle Rock
5 5 Peacock 12 The Office
You can see that we added a new column called shows using the dollar sign.
Using $ to select the column of the data frame
You can select the data frame column using the $ operator.
To select, and print the values of a column in a data frame, use the same way when we worked with a list.
streaming <- data.frame(
service_id = c(1:5),
service_name = c("Netflix", "Disney+", "HBOMAX", "Hulu", "Peacock"),
service_price = c(18, 10, 15, 7, 12),
stringsAsFactors = FALSE
)
print(streaming$service_name)
Output
[1] "Netflix" "Disney+" "HBOMAX" "Hulu" "Peacock"
That is it for the dollar sign in R.
Conclusion
The $ sign is used to access the elements of a list or a data frame by their names in R.
If you have a list A with elements x and y, you can access element a using A$x and A$y.
If you have a data frame df with columns col1 and col2, you can access column col1 using df$col1.
Further reading

Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language.