- The data in R objects (Data Frames and list) can be accessed by row and column indexes, name of the columns, and logical vectors
$operator is used for extracting data from the Data Frame or list by the name of columns (Data Frame) or elements (list). The
$operator does not work with the matrix object.
- In addition, the
$operator is useful for adding or deleting data to existing Data Frame and list.
$is an infix operator which extracts or updates data by taking two arguments (before and after the
$). For example,
dataframe$column1. The detailed information for
$operator can be obtained by
Six example usage of
$ operator for manipulating Data Frame and list is given below,
$ operator to extract columns from Data Frame
Create a random Data Frame,
df <- data.frame(col1 = c("A", "B", "C"), col2 = c(1, 2, 3), col3 = c(0.1, 0.2, 0.3)) # output col1 col2 col3 1 A 1 0.1 2 B 2 0.2 3 C 3 0.3
Now, you have created a Data Frame and want to extract the columns from Data Frame by their name using the
# extract col1 df$col1  "A" "B" "C"
$ operator allows selecting only one column at a time
Add a new column to Data Frame
$ operator is useful to add a new column to the existing Data Frame. You can use a vector to add the data to Data
Frame. The length of the vector should be the same as the number of rows in the Data Frame.
df$col4 <- c(10, 11, 12) df # output col1 col2 col3 col4 1 A 1 0.1 10 2 B 2 0.2 11 3 C 3 0.3 12
Delete column in Data Frame
$ operator can be used to delete a column from Data Frame. You can set the column name to
NULL to delete the
column. The drawback of this method is that you can not delete multiple columns at the same time.
# delete col4 df$col4 <- NULL df # output col1 col2 col3 1 A 1 0.1 2 B 2 0.2 3 C 3 0.3
$ operator to extract elements from list
Create a random list,
list_data <- list(ele1 = c("A", "B", "C"), ele2 = c(1, 2, 3), ele3 = c(0.1, 0.2, 0.3)) list_data # output $ele1  "A" "B" "C" $ele2  1 2 3 $ele3  0.1 0.2 0.3
Now, we have created the list with three elements and want to extract the values of elements by name using the
# access list element ele2 list_data$ele2  1 2 3
Note: You can not extract multiple list elements with
$ operator at the same time. you should use square brackets
) for extracting multiple elements from list (e.g.
Add a new element to the list
$ can be used to update the list and add a new element
list_data$ele4 <- c(10, 11, 12, 14) list_data # output $ele1  "A" "B" "C" $ele2  1 2 3 $ele3  0.1 0.2 0.3 $ele4  10 11 12 14
Delete elements of the list
$ operator can be used to delete the elements of a list by assigning
NULL value to that element. The drawback of
this method is that you can not delete multiple elements of a list at the same time.
# delete ele2 from list list_data$ele2 <- NULL list_data # output $ele1  "A" "B" "C" $ele3  0.1 0.2 0.3 $ele4  10 11 12 14
Enhance your skills with courses on R
- R Programming
- Data Science: Foundations using R Specialization
- Data Analysis with R Specialization
- Getting Started with Rstudio
This work is licensed under a Creative Commons Attribution 4.0 International License
Some of the links on this page may be affiliate links, which means we may get an affiliate commission on a valid purchase. The retailer will pay the commission at no additional cost to you.