Why does R use partial matching?

后端 未结 1 1126
悲&欢浪女
悲&欢浪女 2020-11-28 15:49

I know that for a list, partial matching is done when indexing using the basic operators $ and [[. For example:

ll <- list(yy=1)         


        
相关标签:
1条回答
  • 2020-11-28 16:14

    Partial matching exists to save you typing long argument names. The danger with it is that functions may gain additional arguments later on which conflict with your partial match. This means that it is only suitable for interactive use – if you are writing code that will stick around for a long time (to go in a package, for example) then you should always write the full argument name. The other problem is that by abbreviating an argument name, you can make your code less readable.

    Two common good uses are:

    1. len instead of length.out with the seq (or seq.int) function.

    2. all instead of all.names with the ls function.

    Compare:

    seq.int(0, 1, len = 11) 
    seq.int(0, 1, length.out = 11)
    
    ls(all = TRUE)
    ls(all.names = TRUE)
    

    In both of these cases, the code is just about as easy to read with the shortened argument names, and the functions are old and stable enough that another argument with a conflicting name is unlikely to be added.

    A better solution for saving on typing is, rather than using abbreviated names, to use auto-completion of variable and argument names. R GUI and RStudio support this using the TAB key, and Architect supports this using CTRL+Space.

    0 讨论(0)
提交回复
热议问题