Mark R Mark R - 2 months ago 9
R Question

R cor changes variables names

I've noticed that R changes column names when those could be arguments. For example

foo.df <- data.frame("for" = rnorm(100,0,1), "else"=rnorm(100,2,0.1))
foo.cor.df <- cor(foo.df)


The colnames in the resulting matrix are "for." and "else." suffixed with periods. Is this documented? Is there a solution other than just cleaning up the new columns names?

Answer

for and else are among the reserved words in R and using them as names in a data frame is not valid. See ?data.frame and the check.names argument, which directs you to ?make.names, which has:

Details:

     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.

noting the last line.

The appended . is added to make the names valid.