Search code examples
rr-faq

Variable name restrictions in R


What are the restrictions as to what characters (and maybe other restrictions) can be used for a variable name in R?

(This screams of general reference, but I can't seem to find the answer)


Solution

  • You might be looking for the discussion from ?make.names:

    A syntactically valid name consists of letters, numbers and the dot or underline characters and starts with a letter or the dot not followed by a number. Names such as ".2way" are not valid, and neither are the reserved words.

    In the help file itself, there's a link to a list of reserved words, which are:

    if else repeat while function for in next break

    TRUE FALSE NULL Inf NaN NA NA_integer_ NA_real_ NA_complex_ NA_character_

    Many other good notes from the comments include the point by James to the R FAQ addressing this issue and Josh's pointer to a related SO question dealing with checking for syntactically valid names.