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

前端 未结 2 1706
执笔经年
执笔经年 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.

提交回复
热议问题