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

前端 未结 2 1707
执笔经年
执笔经年 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:27

    Been thinking about this more. Here's a list of one-letter object names in base R:

    > var.names <- c(letters,LETTERS)
    > var.names[sapply(var.names,exists)]
    [1] "c" "q" "t" "C" "D" "F" "I" "T" "X"
    

    And one- and two-letter object names in base R:

    one.letter.names <- c(letters,LETTERS)
    
    N <- length(one.letter.names)
    
    
    first <- rep(one.letter.names,N)
    second <- rep(one.letter.names,each=N)
    
    two.letter.names <- paste(first,second,sep="")
    
    var.names <- c(one.letter.names,two.letter.names)
    
    > var.names[sapply(var.names,exists)]
    [1] "c"  "d"  "q"  "t"  "C"  "D"  "F"  "I"  "J"  "N"  "T"  "X"  "bc" "gc"
    [15] "id" "sd" "de" "Re" "df" "if" "pf" "qf" "rf" "lh" "pi" "vi" "el" "gl"
    [29] "ll" "cm" "lm" "rm" "Im" "sp" "qq" "ar" "qr" "tr" "as" "bs" "is" "ls"
    [43] "ns" "ps" "ts" "dt" "pt" "qt" "rt" "tt" "by" "VA" "UN"
    

    That's a much bigger list than I initially suspected, although I would never think of naming a variable "if", so to a certain degree it makes sense.

    Still doesn't capture object names not in base, or give any sense of which functions are best avoided. I think a better answer would either use expert opinion to figure out which functions are important (e.g. using c is probably worse than using qf) or use a data mining approach on a bunch of R code to see what short-named functions get used the most.

    0 讨论(0)
  • 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)
    
    0 讨论(0)
提交回复
热议问题