R 是否忽略数据框中以点开头的变量名称扩展?
Does R ignore variable name extensions starting with a dot in a data frame?
我有一个数据框,其中一些变量名称包含“.”。扩大。如果我尝试在没有完整变量名的情况下访问它,R 似乎会忽略此扩展并给我变量。是什么导致了 this/why 它发生了吗?下面是我的问题的一个小例子。
y <- rnorm(100)
x <- rlnorm(100)
data <- cbind.data.frame(y,x)
colnames(data) <- c("y.rnorm","x.rlnorm")
# these both return the same thing
data$y
data$y.rnorm
R 被设置为按设计为部分匹配提供结果。
阅读 R 语言定义的 3.4 & 4.3 部分。
3.4.1 Character. The strings in i
are matched against the names attribute of x
and the resulting integers are used. 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. Setting exact to TRUE
prevents partial matching from occurring, a FALSE
value allows it and does not issue any warnings. Note that [
always requires an exact match. The string ""
is treated specially: it indicates ‘no name’ and matches no element (not even those without a name). Note that partial matching is only used when extracting and not when replacing.
和
4.3.2 Partial matching on tags. Each remaining named supplied argument is compared to the remaining formal arguments using partial matching. If the name of the supplied argument matches exactly with the first part of a formal argument then the two arguments are considered to be matched. It is an error to have multiple partial matches. Notice that if f <- function(fumble, fooey) fbody
, then f(f = 1, fo = 2)
is illegal, even though the 2nd actual argument only matches fooey
. f(f = 1, fooey = 2)
is legal though since the second argument matches exactly and is removed from consideration for partial matching. If the formal arguments contain ‘...’ then partial matching is only applied to arguments that precede it.
更新
如 Uwe, there may be a pending update to the R language definition as the behaviour of [[
partial matching has changed. A look through R News 所述,在 3.1.0 版本中,已弃用和失效:
Partial matching when using the $
operator on data frames now throws a warning and may become defunct in the future. If partial matching is intended, replace foo$bar
by foo[["bar", exact = FALSE]]
$
运算符旨在进行部分匹配。参见 the Subsetting chapter of Advanced R by Hadley Wickham,Ctrl + F "partial matching":
There’s one important difference between $ and [[. $ does partial matching:
x <- list(abc = 1)
x$a
## [1] 1
x[["a"]]
## NULL
If you want to avoid this behaviour you can set the global option warnPartialMatchDollar
to TRUE
. Use with caution: it may affect behaviour in other code you have loaded (e.g., from a package).
我有一个数据框,其中一些变量名称包含“.”。扩大。如果我尝试在没有完整变量名的情况下访问它,R 似乎会忽略此扩展并给我变量。是什么导致了 this/why 它发生了吗?下面是我的问题的一个小例子。
y <- rnorm(100)
x <- rlnorm(100)
data <- cbind.data.frame(y,x)
colnames(data) <- c("y.rnorm","x.rlnorm")
# these both return the same thing
data$y
data$y.rnorm
R 被设置为按设计为部分匹配提供结果。
阅读 R 语言定义的 3.4 & 4.3 部分。
3.4.1 Character. The strings in
i
are matched against the names attribute ofx
and the resulting integers are used. 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. Setting exact toTRUE
prevents partial matching from occurring, aFALSE
value allows it and does not issue any warnings. Note that[
always requires an exact match. The string""
is treated specially: it indicates ‘no name’ and matches no element (not even those without a name). Note that partial matching is only used when extracting and not when replacing.
和
4.3.2 Partial matching on tags. Each remaining named supplied argument is compared to the remaining formal arguments using partial matching. If the name of the supplied argument matches exactly with the first part of a formal argument then the two arguments are considered to be matched. It is an error to have multiple partial matches. Notice that if
f <- function(fumble, fooey) fbody
, thenf(f = 1, fo = 2)
is illegal, even though the 2nd actual argument only matchesfooey
.f(f = 1, fooey = 2)
is legal though since the second argument matches exactly and is removed from consideration for partial matching. If the formal arguments contain ‘...’ then partial matching is only applied to arguments that precede it.
更新
如 Uwe, there may be a pending update to the R language definition as the behaviour of [[
partial matching has changed. A look through R News 所述,在 3.1.0 版本中,已弃用和失效:
Partial matching when using the
$
operator on data frames now throws a warning and may become defunct in the future. If partial matching is intended, replacefoo$bar
byfoo[["bar", exact = FALSE]]
$
运算符旨在进行部分匹配。参见 the Subsetting chapter of Advanced R by Hadley Wickham,Ctrl + F "partial matching":
There’s one important difference between $ and [[. $ does partial matching:
x <- list(abc = 1)
x$a
## [1] 1
x[["a"]]
## NULL
If you want to avoid this behaviour you can set the global option
warnPartialMatchDollar
toTRUE
. Use with caution: it may affect behaviour in other code you have loaded (e.g., from a package).