You can abbreviate list names? Why?

做~自己de王妃 提交于 2019-12-04 22:58:50

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.)

For list element names (and function parameter names), R applies the following algorithm:

If there is an exact match for the item, use it. If there is not an exact match, look for partial matches. If there is exactly one partial match, use it. Otherwise, use nothing.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!