What 1-2 letter object names conflict with existing R objects?

前端 未结 2 1743
执笔经年
执笔经年 2021-02-07 11:51

To make my code more readable, I like to avoid names of objects that already exist when creating new objects. Because of the package-based nature of R, and because functions are

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-07 12:36

    apropos is ideal for this:

    apropos("^[[:alpha:]]{1,2}$")
    

    With no packages loaded, this returns:

     [1] "ar" "as" "by" "c"  "C"  "cm" "D"  "de" "df" "dt" "el" "F"  "gc" "gl"
    [15] "I"  "if" "Im" "is" "lh" "lm" "ls" "pf" "pi" "pt" "q"  "qf" "qr" "qt"
    [29] "Re" "rf" "rm" "rt" "sd" "t"  "T"  "ts" "vi"
    

    The exact contents will depend upon the search list. Try loading a few packages and re-running it if you care about conflicts with packages that you commonly use.


    I loaded all the (>200) packages installed on my machine with this:

    lapply(rownames(installed.packages()), require, character.only = TRUE)
    

    And reran the call to apropos, wrapping it in unique, since there were a few duplicates.

    one_or_two <- unique(apropos("^[[:alpha:]]{1,2}$"))
    

    This returned:

      [1] "Ad" "am" "ar" "as" "bc" "bd" "bp" "br" "BR" "bs" "by" "c"  "C" 
     [14] "cc" "cd" "ch" "ci" "CJ" "ck" "Cl" "cm" "cn" "cq" "cs" "Cs" "cv"
     [27] "d"  "D"  "dc" "dd" "de" "df" "dg" "dn" "do" "ds" "dt" "e"  "E" 
     [40] "el" "ES" "F"  "FF" "fn" "gc" "gl" "go" "H"  "Hi" "hm" "I"  "ic"
     [53] "id" "ID" "if" "IJ" "Im" "In" "ip" "is" "J"  "lh" "ll" "lm" "lo"
     [66] "Lo" "ls" "lu" "m"  "MH" "mn" "ms" "N"  "nc" "nd" "nn" "ns" "on"
     [79] "Op" "P"  "pa" "pf" "pi" "Pi" "pm" "pp" "ps" "pt" "q"  "qf" "qq"
     [92] "qr" "qt" "r"  "Re" "rf" "rk" "rl" "rm" "rt" "s"  "sc" "sd" "SJ"
    [105] "sn" "sp" "ss" "t"  "T"  "te" "tr" "ts" "tt" "tz" "ug" "UG" "UN"
    [118] "V"  "VA" "Vd" "vi" "Vo" "w"  "W"  "y"
    

    You can see where they came from with

    lapply(one_or_two, find)
    

提交回复
热议问题