What is dollar $ in R code and how to use it? Dollar sign $ in R Programming is an operator that is used to access, add, update, and delete elements from the named list, and also it is used to access, add, update, and delete columns from the R Dat aFrame.
1. What is Dollar Sign in R?
The Dollar $ sign in R code is a special operator in R Programming Language that is used to access the List for DataFrame. Similar to bracket [] notation, you can use $ sign to access, add, update and delete variables from list and columns from DataFrame.
2. Using Dollar with List in R
In R, List objects are used to store elements of different types like numbers, strings, vectors, and even lists. The good thing in R is, elements in List can be given names and they can be accessed using these names.
By using the dollar sign in R we can access the list element by name, add elements by name, and update elements.
# Create List
rList <- list(emp_id=c(1,2,3,4,5,6),
name=c("Smith","Rose","Williams"),
dept_id=c(10,20,10,10,40,50))
rList
Yields below output. Notice that each element in the list is represented with a $ sign as a prefix to the element name.
2.1 Access Element by Name Using $ Sign
The $ in R allows you to extract or access elements by name from a named list. names() function is used to get the names from the names list. In the following example, I am accessing named elements emp_id
and name
from the rList
object using the $ dollar sign.
# Access named element from list
rList$emp_id
rList$name
Yields below output
2.2 Add New Element to List Using $ Sign
By using the dollar $ sign you can add or insert a new element to the list. In the following example, I am adding dept_name
as named list to the rList
object. If a named element you are trying to add already exists in the list then it updates the values to the exists list.
# Add new element to the list
rList$dept_name <- c("Finance","Marketing","Sales","IT")
rList
Yields below output. Notice that the new element dept_name
has been added to the list.
2.3 Delete Element from List
In order to delete the variable or name from the list assign NULL to the name by referring to using a dollar sign. Fpr example.
# Delete element from list
rList$name <- NULL
rList
I will leave this to you to run and explore the output.
2.4 Accessing Names with Space
Our named list doesn’t have variables with spaces however while referring names by using $ operator, you can try enclosing the name with single quotes. For example.
rList$'dept name' <- c("Finance","Marketing","Sales","IT")
rList$'dept name'
3. Using Dollar with DataFrame in R Code
You can also use dollar sign in R with DataFrame, by using $ sign you can access the column, update the column, add the columnd and delete the column from the DataFrame.
Let’s create an R DataFrame, run these examples and explore the output. If you already have data in CSV you can easily import CSV files to R DataFrame. Also, refer to Import Excel File into R.
# Create emp Data Frame
emp_df=data.frame(
emp_id=c(1,2,3,4,5,6),
name=c("Smith","Rose","Williams","Jones","Brown","Brown"),
dept_id=c(10,20,10,10,40,50)
)
emp_df
# Output
emp_id name dept_id
1 1 Smith 10
2 2 Rose 20
3 3 Williams 10
4 4 Jones 10
5 5 Brown 40
6 6 Brown 50
3.1 Add Column to DataFrame
To add a column to the DataFrame, create a vector witht the values and assign it to the DataFrame by using $ sign in R. The following example adds a column dept_branch_id
to the Data Frame.
# Add column
emp_df$dept_branch_id <- c(101,102,101,101,104,105)
emp_df
# Output
emp_id name dept_id dept_branch_id
1 1 Smith 10 101
2 2 Rose 20 102
3 3 Williams 10 101
4 4 Jones 10 101
5 5 Brown 40 104
6 6 Brown 50 105
3.2 Access Column using Dollar in R
To access the single column from the DataFrame use the $ operator in a form dataframe$columnname
. You can also select column by using the bracket notation[] or using select() function from dplyr package.
# Access column
emp_df$dept_branch_id
# Output
# [1] 101 102 101 101 104 105
3.3 Delete Column using Dollar Sign
Similarly, assign NULL to remove the column from DataFrame.
# Delete column
emp_df$dept_branch_id <- NULL
emp_df
# Output
emp_id name dept_id
1 1 Smith 10
2 2 Rose 20
3 3 Williams 10
4 4 Jones 10
5 5 Brown 40
6 6 Brown 50
4. Complete Example of Using Dollar in R Code
#Create list
rList <- list(emp_id=c(1,2,3,4,5,6),
name=c("Smith","Rose","Williams"),
dept_id=c(10,20,10,10,40,50))
rList
# Access named element from list
rList$emp_id
rList$name
# Add new element to the list
rList$dept_name <- c("Finance","Marketing","Sales","IT")
rList
# Delete element from list
rList$name <- NULL
rList
# Create emp Data Frame
emp_df=data.frame(
emp_id=c(1,2,3,4,5,6),
name=c("Smith","Rose","Williams","Jones","Brown","Brown"),
dept_id=c(10,20,10,10,40,50)
)
emp_df
# Add column
emp_df$dept_branch_id <- c(101,102,101,101,104,105)
emp_df
# Access column
emp_df$dept_branch_id
# Delete column
emp_df$dept_branch_id <- NULL
emp_df
You can also find the complete example at R GitHub Project.
5. Conclusion
In this article, you have learned what is dollar $ operator in R code and by using it how we can access, update, add and element elements from the list. And using $ sign you can also access, update, add and delete columns from DataFrame.
Related Articles
- FUN in R – Usage with Example
- R ifelse() Usage with Examples
- R if else Multiple Conditions
- For Loop in R With Examples
- %NOTIN% Operator in R
- Usage of %in% Operator in R
- Dates and Times in R with Examples