In this very short tutorial, you will learn by example how to use the operator $ in R. First, we will learn what the $ operator does by getting the answer to some frequently asked questions. Second, we will work with a list that we create, and use the dollar sign operator to both select and add a variable. Here you will also learn about the downsides of using $ in R as well as the alternatives that you can use. In the following section, we will also work with a dataframe. Both sections will involve creating the list and the dataframe.

two methods to use the $ operator in R
  • Save
$ in R

Requirements

To follow this post you need a working installation of the R statistical environment, of course. If you want to read the example Excel file you will also need the readxl package.

What does $ do in R?

The $ operator can be used to select a variable/column, to assign new values to a variable/column, or to add a new variable/column in an R object. This R operator can be used on e.g. lists, and dataframes. For example, if we want to print the values in the column “A” in the dataframe called “dollar” we can use the following code: print(dollar$A),

What is the difference between [[]] and $ in R?

First of all, using the double brackets enables us to e.g. select multiple columns whereas the $ operator only enables us to select one column.

Before we go on to the next section, we will create a list using the list() function.

dollar <- list(A = rep('A', 5), B = rep('B', 5), 'Life Expectancy' = c(10, 9, 8, 10, 2))
Code language: R (r)
printing a list in R
  • Save
List in R

In the next section, we will, then, work with the $ operator to 1) add a new variable to the list, and 2) print a variable in the list. In the third example, we will learn how to use $ in R to select a variable which variable contains whitespaces. 

Using $ in R on a List

Here we will start learning, by examples, how to work with the $ operator in R. First, however, we will create a list. 

Example 1: Using the $ Operator to Add a New Variable to a List

Here’s how to use $ in R to add a new variable to a list:

dollar$Sequence <- seq(1, 5)
Code language: R (r)

Notice how we used the name of the list, then the $ operator, and the assignment (“<-”) operator. On the left side of <- we used seq() function to generate a sequence of numbers in R. This sequence of numbers was added to the list. Here’s our example list with the new variable:

add object to list with $ in R
  • Save
List with added column

In the next example, we will use the $ operator to print the values of the new variable that we added. 

Example 2: How to use the $ Operator to Select a Variable in a List

Here’s how we can use $ in R to select a variable in a list:

dollar$Sequence
Code language: R (r)

Again, we used the list name, and the $ operator to print the new column we previously added:

  • Save
Select a variable using $ in R

Note, that 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. Another option to select columns is, of course, using the select() function from the excellent package dplyr. 

You might also be interested in: How to use %in% in R: 7 Example Uses of the Operator

Example 3: How to Select an Object containing White Spaces using $ in R

Here’s how we can print, or select, a variable with white space in the name:

dollar$`Life Expectancy`
Code language: R (r)

Notice how we used the ` in the code above. This way, we can select, or add values, even though the variable contains white space. I would, however, suggest that you rename the column (or replace the white spaces). See the recent post to learn how to rename columns in R. Again, using brackets, in this case, would be the same as when the variable is not containing white spaces. 

In the next section, we will use the same examples above but on a dataframe. First, however, we will read an .xlsx file in R using the readxl package

dataf <- read_excel('example_sheets.xlsx', skip=2)
Code language: R (r)

Note, that we used the skip argument to skip the first two rows. In the example data (download here), the column names are on the third row. We can print the first 5 rows of the dataframe using the head() function:

we can also use $ in R to select columns from dataframes.
  • Save
First 5 rows of dataframe

Here we can see that there are 5 columns. In the next section, we will use the $ operator on this dataframe.

How to use $ in R on a Dataframe

In the first example, we will add a new column to the dataframe. After this, we will select the new column and print it using the $ operator. Finally, we will also add a new example on how to use this operator: to remove a column. 

Example 4: Using $ to Add a new Column to a Dataframe

Here’s how we can use $ to add a new column in R:

dataf$NewData <- rep('A', length(dataf$ID))
Code language: R (r)

Notice how we used  R’s rep() function to generate a vector containing the letter ‘A’. It is important that we generate a vector of the same length as the number of rows in our dataframe. Therefore, we used the length() function as the second argument.

use $ in R to add a new column to dataframe
  • Save
Add column to dataframe with $ in R

Now, if you want to learn easier ways to add a column in R check the following posts:

In the next example, we are going to select this column using the $ operator and print it.

Example 5: Using $ to Select and Print a Column

Here’s how we select and print the values in the column we created:

dataf$NewData
Code language: R (r)

Notice, to select, and print the values, of a column in a dataframe we used R’s $ operator the same way as we used it when we worked with a list. Here’s the output of the code above:

  • Save
Using $ in R to select and print a column

Now, it is easier to use the R package dplyr to select certain columns in R compared to using the $ operator. Another option is, of course, to use the double brackets.

In the next example, we are going to drop a column from the dataframe.

Example 6: Using $ in R together with NULL to delete a column

Here’s how we can delete a column using the $ operator and the NULL object:

dataf$NewData <- NULL
Code language: PHP (php)

Again, we can use the R package dplyr to remove columns. More specifically, we can make use of the select() function to delete multiple columns in a quick and easy way. 

Note, that example 3 will also work if we have a column containing white spaces in our dataframe. Finally, before concluding this post, we will have a quick look on how to use brackets to select a column:

dataf['ID']
Code language: R (r)

Notice how we used the column name of the variable we wanted to select. This, again, will work on a list as well. 

Conclusion:

In this post, you have learned, by examples, how to use $ in R. First, we worked with a list to add a new variable and select a variable. Then, we used the same methods on a dataframe. As a bonus, we also had a look at how to remove a column using the $ operator. Hope you learned something. If you did please share the post in your work, on your social media accounts, or link back to it in your own blog posts. If you have any comments or suggestions to the post please leave a comment below. 

  • Save
Share via
Copy link
Powered by Social Snap