对于命名向量和矩阵,[[ 是否在不传递 exact=FALSE 参数的情况下使用部分匹配?
For named vectors and matrices, does [[ ever use partial matching without passing the exact=FALSE argument?
我最近读了 this section of the R Language Definition,变得很困惑。它说:
For vectors and matrices the [[
forms are rarely used, although they have some slight semantic differences from the [
form (e.g. it drops any names or dimnames attribute, and that partial matching is used for character indices).
我看错了吗?似乎说 [[
允许对命名向量和矩阵进行部分匹配(大概是默认情况下)。但是,我知道这显然是错误的:
> lett<-setNames(sample(26),paste0(letters,letters,letters))
> lett[["aaa"]]#No partial match
[1] 23
> lett[["a"]]#Error
Error in lett[["a"]] : subscript out of bounds
那么语言定义是什么意思?它可能是在谈论您可以设置的 exact=FALSE
标志,但这似乎是一种非常奇怪的方式。事实上,a later section 提出了以下声明,我上面的代码反驳了这一点,所以我更加困惑:
For [[
and $
partial matching is used if exact matching fails, so x$aa
will match x$aabb
if x
does not contain a component named "aa" and "aabb" is the only name which has prefix "aa". For [[
, partial matching can be controlled via the exact argument which defaults to NA
indicating that partial matching is allowed, but should result in a warning when it occurs.
语言定义是否过时了?
实际上,我认为语言定义 - 至少部分 - 确实过时了。 help("[[")
关于 exact
参数的帮助页面指出
Controls possible partial matching of [[ when extracting by a character vector [...]. The default is no partial matching. Value NA allows partial matching but issues a warning when it occurs. Value FALSE allows partial matching without any warning.
用法支持这一说法:
x[[i, exact = TRUE]]
x[[i, j, ..., exact = TRUE]]
以下代码也证明了这些默认值。
set.seed(1)
lsub <- letters[1:3]
lett <- setNames(lapply(sample(3), c), paste0(lsub, lsub, lsub))
lett
#> $aaa
#> [1] 1
#>
#> $bbb
#> [1] 3
#>
#> $ccc
#> [1] 2
# partial matching
lett$a
#> [1] 1
lett[["aa", exact = FALSE]]
#> [1] 1
# no partial matching
lett[["aa"]]
#> NULL
# partial matching with warning
lett[["aa", exact = NA]]
#> Warning in lett[["aa", exact = NA]]: partial match of 'aa' to 'aaa'
#> [1] 1
我最近读了 this section of the R Language Definition,变得很困惑。它说:
For vectors and matrices the
[[
forms are rarely used, although they have some slight semantic differences from the[
form (e.g. it drops any names or dimnames attribute, and that partial matching is used for character indices).
我看错了吗?似乎说 [[
允许对命名向量和矩阵进行部分匹配(大概是默认情况下)。但是,我知道这显然是错误的:
> lett<-setNames(sample(26),paste0(letters,letters,letters))
> lett[["aaa"]]#No partial match
[1] 23
> lett[["a"]]#Error
Error in lett[["a"]] : subscript out of bounds
那么语言定义是什么意思?它可能是在谈论您可以设置的 exact=FALSE
标志,但这似乎是一种非常奇怪的方式。事实上,a later section 提出了以下声明,我上面的代码反驳了这一点,所以我更加困惑:
For
[[
and$
partial matching is used if exact matching fails, sox$aa
will matchx$aabb
ifx
does not contain a component named "aa" and "aabb" is the only name which has prefix "aa". For[[
, partial matching can be controlled via the exact argument which defaults toNA
indicating that partial matching is allowed, but should result in a warning when it occurs.
语言定义是否过时了?
实际上,我认为语言定义 - 至少部分 - 确实过时了。 help("[[")
关于 exact
参数的帮助页面指出
Controls possible partial matching of [[ when extracting by a character vector [...]. The default is no partial matching. Value NA allows partial matching but issues a warning when it occurs. Value FALSE allows partial matching without any warning.
用法支持这一说法:
x[[i, exact = TRUE]]
x[[i, j, ..., exact = TRUE]]
以下代码也证明了这些默认值。
set.seed(1)
lsub <- letters[1:3]
lett <- setNames(lapply(sample(3), c), paste0(lsub, lsub, lsub))
lett
#> $aaa
#> [1] 1
#>
#> $bbb
#> [1] 3
#>
#> $ccc
#> [1] 2
# partial matching
lett$a
#> [1] 1
lett[["aa", exact = FALSE]]
#> [1] 1
# no partial matching
lett[["aa"]]
#> NULL
# partial matching with warning
lett[["aa", exact = NA]]
#> Warning in lett[["aa", exact = NA]]: partial match of 'aa' to 'aaa'
#> [1] 1