How to use $ operator in Data Frame and list in R (with six examples)

Renesh Bedre    3 minute read

Introduction

  • The data in R objects (Data Frames and list) can be accessed by row and column indexes, name of the columns, and logical vectors
  • The $ 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 help('$') command.

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 $ operator,

# extract col1
df$col1
[1] "A" "B" "C"

Note: $ 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
[1] "A" "B" "C"

$ele2
[1] 1 2 3

$ele3
[1] 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 $ operator,

# access list element ele2
list_data$ele2
[1] 1 2 3

Note: You can not extract multiple list elements with $ operator at the same time. you should use square brackets operator ([]) for extracting multiple elements from list (e.g. list_data[c('ele1', 'ele2')]).

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
[1] "A" "B" "C"

$ele2
[1] 1 2 3

$ele3
[1] 0.1 0.2 0.3

$ele4
[1] 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
[1] "A" "B" "C"

$ele3
[1] 0.1 0.2 0.3

$ele4
[1] 10 11 12 14

Enhance your skills with courses on R

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.

Tags:

Updated: