Dollar In R: Sign($) How To Use

dollar in r

How does R code use Dollar $, and what does it mean? The operator $ is used in R programming to access, add, update, and delete elements from the named list or dataframe. Follow this article to get helpful information.

What is a Dollar in R?

We can add, update, and delete elements and access variables in lists and data frames using the Dollar symbol ($) operator in R.

The examples that follow illustrate typical applications for this operator.

Dollar in R: The Example

Using Dollar to create a variable in a list

First, we need to create a list as shown below:

# Create a list
myList <- list(
    a = c("A", "B", "C"),
    b = c(12,20),
    d = 10:20
)

# Print list
myList

Output

$a
[1] "A" "B" "C"

$b
[1] 12 20

$d
[1] 10 11 12 13 14 15 16 17 18 19 20

Now, we will use the Dollar to create a variable in the list.

# Create a list
myList <- list(
    a = c("A", "B", "C"),
    b = c(12,20),
    d = 10:20
)

myList$e <- c("D","E","F")

# Print list
myList

Output

$a
[1] "A" "B" "C"

$b
[1] 12 20

$d
[1] 10 11 12 13 14 15 16 17 18 19 20

$e
[1] "D" "E" "F"

You can see that we have a new e-variable in this list.

Using Dollar to access a variable in a list

If you want to access a specific variable in the list, you can use Dollar sign.

Following the code below:

# Create a list
myList <- list(
    a = c("A", "B", "C"),
    b = c(12,20),
    d = 10:20
)

# Access b variable
myList$b

Output

[1] 12 20

Using Dollar to delete a column in a list

We will access this variable and assign it a NULL value to delete a specific variable in a list.

# Create a list
myList <- list(
    a = c("A", "B", "C"),
    b = c(12,20),
    d = 10:20
)

# Delete b variable
myList$b <- NULL

# Print list
myList

Output

$a
[1] "A" "B" "C"

$d
[1] 10 11 12 13 14 15 16 17 18 19 20

Using Dollar to create a variable in a data frame

Similarly, we need to create a dataframe:

# Create a dataframe
df <- data.frame(
    id= 1:5,
    math = c(8,6,9,7,4),
    english = c(10,5,7,5,8)
)

# Print data frame
df

Output

  id math english
1  1    8      10
2  2    6       5
3  3    9       7
4  4    7       5
5  5    4       8

Here, we will use Dollar sign to create a variable in this data frame.

# Create a dataframe
df <- data.frame(
    id= 1:5,
    math = c(8,6,9,7,4),
    english = c(10,5,7,5,8)
)

df$physics <- c(9,7,6,9,8)

# Print data frame
df

Output

  id math english physics
1  1    8      10       9
2  2    6       5       7
3  3    9       7       6
4  4    7       5       9
5  5    4       8       8

Using Dollar to access a variable in a data frame

For instance, the English variable in the data frame can be accessed using the code below:

# Create a dataframe
df <- data.frame(
    id= 1:5,
    math = c(8,6,9,7,4),
    english = c(10,5,7,5,8)
)

# Access english variable
df$english

Output

[1] 10  5  7  5  8

Using Dollar to delete a column in a data frame

To delete a column in a data frame, we will access this column and assign it a NULL value.

# Create a dataframe
df <- data.frame(
    id= 1:5,
    math = c(8,6,9,7,4),
    english = c(10,5,7,5,8)
)

# Delete english column
df$english <- NULL

# Print data frame
df

Output

  id math
1  1    8
2  2    6
3  3    9
4  4    7
5  5    4

Summary

Thank you for reading this article. We hope after reading this article, you will understand more about the dollar in R. If you have any questions, please leave a comment below. We will answer as possible.

Good luck, everyone!

Maybe you are interested:

Posted in R

Leave a Reply

Your email address will not be published. Required fields are marked *