Difference Between Single & Double Square Brackets in R (3 Examples)

 

In this article you’ll learn how to use single and double square brackets in R programming.

The article consists of this:

You’re here for the answer, so let’s get straight to the R syntax:

 

Example 1: Accessing Elements of Vectors

In this Example, I’ll explain how to access the elements of a vector (or array) in R.

First, we have to create an example vector:

my_vector <- 1:7                   # Create example vector
my_vector                          # Print example vector
# 1 2 3 4 5 6 7

Now, we can use a single square bracket to extract a specific element of our vector:

my_vector[3]                       # Access vector element
# 3

 

Example 2: Accessing Elements of Data Frames

In this Example, I’ll show how to subset data frames using single square brackets.

Let’s create some example data:

my_data <- data.frame(x1 = 5:9,    # Create example data frame
                      x2 = letters[5:1],
                      x3 = "X")
my_data                            # Print example data frame
#   x1 x2 x3
# 1  5  e  X
# 2  6  d  X
# 3  7  c  X
# 4  8  b  X
# 5  9  a  X

As you can see based on the previous output of the RStudio console, our example data frame consists of five rows and three columns.

Now, we can use single square brackets and a comma to extract certain elements of our data frame. Note that the index before the comma specifies the row and the index after the comma specifies the column:

my_data[3, 1]                      # Access data frame element using indices
# 7

The data frame cell in the third row and the first variable contains the value 7.

Alternatively, we can also use the $-operator in combination with single square brackets:

my_data$x1[3]                      # Access data frame element using $-operator
# 7

 

Example 3: Accessing Elements of Lists

In Example 3, I’ll show how to extract certain list elements using single and double square brackets.

Let’s create an example list:

my_list <- list(1:5,               # Create example list
                "ABC",
                LETTERS[5:9])
my_list                            # Print example list
# [[1]]
# [1] 1 2 3 4 5
# 
# [[2]]
# [1] "ABC"
# 
# [[3]]
# [1] "E" "F" "G" "H" "I"

As you can see, our list contains three list elements.

We can use single square brackets to extract a specific list element of our list:

my_list[3]                         # Access list element using []
# [[1]]
# [1] "E" "F" "G" "H" "I"

Note that this list subset is still a list (as you can see based on the [[1]] in the output).

If we want to extract a list element with its own data type, we can use double square brackets:

my_list[[3]]                       # Access list element using [[]]
# [1] "E" "F" "G" "H" "I"

This output is a vector.

 

Video & Further Resources

Have a look at the following video of my YouTube channel. In the video, I’m explaining the R code of this tutorial:

 

The YouTube video will be added soon.

 

Also, you may read the related tutorials on my website. A selection of articles about selecting data is listed below.

 

In this R tutorial you learned how to apply [] and [[]]. Don’t hesitate to tell me about it in the comments section, if you have additional questions. Furthermore, don’t forget to subscribe to my email newsletter in order to get updates on new tutorials.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Top