您可以缩写列表名称吗?为什么? [英] You can abbreviate list names? Why?
问题描述
这让我很糟糕.您可以缩写列表名称吗?我以前从没注意到它,我整整一天都被搞砸了.有人可以解释这里发生的事情以及为什么它比可怕的事情有用吗?以及为什么它的底部如此不一致?如果我可以将其关闭?
> wtf <- list(whatisthe=1, pointofthis=2)
> wtf$whatisthe
[1] 1
> wtf$what
[1] 1
> wtf <- list(whatisthe=1, whatisthepointofthis=2)
> wtf$whatisthepointofthis
[1] 2
> wtf$whatisthep
[1] 2
> wtf$whatisthe
[1] 1
> wtf$what
NULL
我怀疑$
运算符的部分匹配在实现选项卡式完成之前的日子里是一种很好的交互式使用功能>
如果您不喜欢这种行为,则可以使用"[["
运算符.
它带有参数exact=
,该参数允许您控制部分匹配行为,并且默认为TRUE
.
wtf[["whatisthep"]] # Only returns exact matches
# NULL
wtf[["whatisthep", exact=FALSE]] # Returns partial matches without warning
# [1] 2
wtf[["whatisthep", exact=NA]] # Returns partial matches & warns that it did
# [1] 2
# Warning message:
# In wtf[["whatisthep", exact = NA]] :
# partial match of 'whatisthep' to 'whatisthepointofthis'
(这是为什么R编程中通常首选"[["
优于$
的一个原因.另一个原因是能够执行此X <- "whatisthe"; wtf[[X]]
而不是执行此X <- "whatisthe"; wtf$X
的能力.)
This got me pretty bad. You can abbreviate list names? I never noticed it before and I got totally screwed for like a day. Can someone explain what is happening here and why it could be more useful than it is terrible? And why its inconsistent like that at the bottom? And if I can turn it off?
> wtf <- list(whatisthe=1, pointofthis=2)
> wtf$whatisthe
[1] 1
> wtf$what
[1] 1
> wtf <- list(whatisthe=1, whatisthepointofthis=2)
> wtf$whatisthepointofthis
[1] 2
> wtf$whatisthep
[1] 2
> wtf$whatisthe
[1] 1
> wtf$what
NULL
I suspect that partial matching by the $
operator was a nice(r) feature for interactive use back in the days before tabbed completion had been implemented
If you don't like that behavior, you can use the "[["
operator instead.
It takes an argument exact=
, which allows you to control partial matching behavior, and which defaults to TRUE
.
wtf[["whatisthep"]] # Only returns exact matches
# NULL
wtf[["whatisthep", exact=FALSE]] # Returns partial matches without warning
# [1] 2
wtf[["whatisthep", exact=NA]] # Returns partial matches & warns that it did
# [1] 2
# Warning message:
# In wtf[["whatisthep", exact = NA]] :
# partial match of 'whatisthep' to 'whatisthepointofthis'
(This is one reason why "[["
is generally preferred to $
in R programming. Another is the ability to do this X <- "whatisthe"; wtf[[X]]
but not this X <- "whatisthe"; wtf$X
.)
这篇关于您可以缩写列表名称吗?为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!