你可以缩写列表名称吗?为什么?

enf*_*ion 16 r list names

这让我非常糟糕.你可以缩写列表名称吗?我之前从未注意到它,而且我完全搞砸了一天.有人可以解释这里发生了什么,为什么它可能比它可怕更有用?为什么它与底部的不一致?如果我可以关掉它?

> 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  
Run Code Online (Sandbox Code Playgroud)

Jos*_*ien 16

我怀疑$运营商的部分匹配是一个很好的(r)功能,用于在标签式完成实施之前的几天内进行交互式使用

如果您不喜欢该行为,则可以使用"[["运算符.它需要一个参数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'
Run Code Online (Sandbox Code Playgroud)

(这是R编程中"[["通常首选的一个原因$.另一个原因是能够做到这一点X <- "whatisthe"; wtf[[X]]而不是这个X <- "whatisthe"; wtf$X.)

  • 进一步思考,我可以看到,在实现选项卡式自动完成之前,它在交互式使用中会有所帮助. (2认同)