单方括号和双方括号在 R 编程语言中用作索引运算符。这两个运算符都用于将 R 存储对象的组件作为属于相同数据类型的子集或作为元素进行引用。
下表说明了两种运算符之间的一些主要区别:
[] |
[[]] |
---|---|
Used for singular indexing | Used for recursive indexing |
Access list within a list | Access elements within a list |
It may or may not return a single element from the list | It always returns a single element from the list |
Allows indexing by vectors | Allows indexing by integers or characters |
示例 1:访问单数列表的元素
单一列表包含元素的单一索引级别。在应用任一方括号运算符,返回列表指定索引位置处的元素。在双方括号的情况下,获得相同的结果,因为第 j 个指定索引处的列表也由该特定元素本身组成。因此,括号的类型可以互换使用。这里唯一的区别是,如果我们指定的索引大于列表的长度,则单括号返回 NA,因为缺少该值。但是,双括号尝试访问此缺失索引处的列表,从而引发错误。
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]])
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]])
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]])
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]])
输出:
[1] "Original Vector"
[1] 1 2 3 4 5 6 7 8 9 10
[1] "Singular brackets"
[1] 5
[1] "Double brackets"
[1] 5
示例 2:访问嵌套列表的元素
嵌套列表包含列表和其他对象(向量)作为其元素。嵌套列表中的检索是基于多重和递归索引执行的。用于参考的索引下标的数量取决于为访问元素而探索的级别数。索引处的元素可以使用双括号访问,列表的子集可以使用单括号访问。
电阻
# 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]])
输出:
[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"
示例 3:访问向量或数组的元素
对于一维数组,两个运算符的工作方式没有重大差异。这两个运算符试图返回存在于数组指定索引处的元素。但是,与单数列表一样,如果访问范围外的任何元素,则在单括号的情况下返回 NA,而双括号则抛出下标越界异常,同时终止执行。
电阻
# declaring a numeric vector
arr <- 1:15
print ("Original Vector")
print (arr)
print ("Singular brackets")
print(arr[8])
print ("Double brackets")
print(arr[[8]])
输出
[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
示例 4:访问数据帧的元素
数据帧中的单个括号用于访问数据帧的子集,即仅访问子数据帧。与指定索引对应的列和所有行作为输出返回。如果数据帧中有双括号,相同的输出将返回为元素向量的形式。
电阻
# 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]])
输出:
[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