Skip to content
Related Articles

Related Articles

Improve Article

Difference Between Single and Double Square Brackets in R

  • Last Updated : 16 May, 2021

The single and double square brackets are used as indexing operators in R Programming Language. Both of these operators are used for referencing the components of R storage objects either as a subset belonging to the same data type or as an element. 

The table illustrates some key differences among both the types of operators : 

[]

[[]]

Used for singular indexingUsed for recursive indexing
Access list within a listAccess elements within a list
It may or may not return a single element from the listIt always returns a single element from the list
Allows indexing by vectorsAllows indexing by integers or characters

Example 1: Accessing the elements of the singular lists



Singular lists contain a single level of indexing for the elements. On the application of either of the bracket operator, the elements at the specified index position of the list is returned. In case of double square brackets, the same result is obtained, because the list at jth specified index also consist of that particular element itself. Therefore, the type of brackets can be used interchangeably. The only difference here is, in case we specify an index which is greater than the length of the list, then singular brackets return NA, because the value is missing. However, double brackets try to access the list present at this missing index, and thereby throws an error.

R




# declaring a numeric vector
vec <- c(1:10)
print ("Original Vector")
print (vec)
  
# using single bracket
print ("Singular brackets")
print(vec[5])
  
# usind dual bracket
print ("Double brackets")
print (vec[[5]])

Output:

[1] "Original Vector"
[1]  1  2  3  4  5  6  7  8  9 10
[1] "Singular brackets"
[1] 5
[1] "Double brackets"
[1] 5

Example 2: Accessing the elements of the nested lists

Nested lists contain lists and other objects (either vectors) as its elements. Retrieval in the nested lists is performed based on multiple and recursive indexing. The number of index subscripts used for reference depends on the number of levels to explore to access the element. The element at the index can be accessed using double brackets and lists’ subset can be accessed using single brackets. 

R




# declaring a nested list
nested_list <- list(list(letters[1:8],"secondele"),              
                5:15,
                FALSE)
  
print("Original list")
print(nested_list)
  
print ("Accessing first sub-list of the list")
print(nested_list[1])
  
print ("Accessing first component of the list")
print(nested_list[[1]])
  
print ("Accessing components inside component of the list")
print(nested_list[[1]][[2]])

Output:

[1] "Original list"
[[1]]
[[1]][[1]]
[1] "a" "b" "c" "d" "e" "f" "g" "h"

[[1]][[2]]
[1] "secondele"


[[2]]
 [1]  5  6  7  8  9 10 11 12 13 14 15

[[3]]
[1] FALSE

[1] "Accessing first sub-list of the list"
[[1]]
[[1]][[1]]
[1] "a" "b" "c" "d" "e" "f" "g" "h"

[[1]][[2]]
[1] "secondele"


[1] "Accessing first component of the list"
[[1]]
[1] "a" "b" "c" "d" "e" "f" "g" "h"

[[2]]
[1] "secondele"

[1] "Accessing components inside component of the list"
[1] "secondele"

Example 3: Accessing the elements of the vectors or arrays



In case of uni-dimensional arrays, there is no major differences in the working of both the operators. Both the operators attempt to return the element present at the specified index of the array. However, like singular lists, in case any element outside the range is accessed, in case of singular brackets NA is returned, while double brackets throw a subscript out of bounds exception, while terminating the execution. 

R




# declaring a numeric vector
arr <- 1:15
print ("Original Vector")
print (arr)
  
print ("Singular brackets")
print(arr[8])
  
print ("Double brackets")
print(arr[[8]])

Output

[1] "Original Vector"
[1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
[1] "Singular brackets"
[1] 10
[1] "Singular brackets"
[1] 10

Example 4: Accessing the elements of the dataframes

The single brackets in a dataframe are used to access a subset of the dataframe, that is a sub-dataframe is only accessed. The column corresponding to the specified index and all the rows are returned as an output. In case of double brackets in a dataframe, the same output is returned to the form of an element vector. 

R




# declaring a dataframe
data_frame <- data.frame(col1=letters[1:6], col2 = c(7:12),
                            col3 = c(1:6))
print ("Original dataframe")
print (data_frame)
  
print ("Sub dataframe")
print (data_frame[2])
  
print ("Element dataframe")
print (data_frame[[2]])

Output:

[1] "Original dataframe"
 col1 col2 col3
1    a    7    1
2    b    8    2
3    c    9    3
4    d   10    4
5    e   11    5
6    f   12    6
[1] "Sub dataframe"
 col2
1    7
2    8
3    9
4   10
5   11
6   12
[1] "Element dataframe"
[1]  7  8  9 10 11 12



My Personal Notes arrow_drop_up
Recommended Articles
Page :