Dollar sign in R with Example

There are four forms of the extract operator in R: [, [[, $, and @. The fourth form is also known as 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 that we won’t discuss the slot operator here.

The first form, [, can be used to 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([[]]) enables us to select multiple columns, whereas the $ operator only enables us to select one column.

Dollar sign in R

To access a variable in the R dataset, use the dollar sign “$”. The $ operator can be used to 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 in 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 have to use the double brackets and put in each column name as a character.

How to use $ operator 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 $ 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 dollar sign in R tutorial.

Leave a Comment