我是R的新手,正在尝试以下代码.令我惊讶的是,分配的内容ret$log.id实际上也会导致分配给相同的值ret$log.例如,
ret <- c()
ret$log.id <- 'a'
Run Code Online (Sandbox Code Playgroud)
运行以下将返回 "a"
ret$log
Run Code Online (Sandbox Code Playgroud)
这是R应该做的吗?我希望有人可以给我一些洞察力.
谢谢,
是的,$运营商正在做一些部分匹配.您可以使用以下内容稍微探索一下此行为:
ret <- c()
ret$log.id <- "a"
ret$l #Returns "a"
ret$log.at <- "b"
Run Code Online (Sandbox Code Playgroud)
现在看看返回的内容如下:
ret$l
ret$log
ret$log.i
ret$log.a
Run Code Online (Sandbox Code Playgroud)
详细说明部分匹配的boner.从帮助页面$:
根据论据:
name A literal character string or a name (possibly backtick quoted).
For extraction, this is normally (see under ‘Environments’) partially matched to the names
of the object.
Run Code Online (Sandbox Code Playgroud)
然后在字符索引下:
Character indices can in some circumstances be partially matched (see pmatch) to the
names or dimnames of the object being subsetted (but never for subassignment).
Run Code Online (Sandbox Code Playgroud)
也在字符索引下:
Thus the default behaviour is to use partial matching only when extracting from
recursive objects (except environments) by $. Even in that case, warnings can be
switched on by options(warnPartialMatchAttr = TRUE).
Run Code Online (Sandbox Code Playgroud)
有更多细节参考names,pmatch但我已经清除了它.