21

Is there a reason why R won't allow me to have a number as the column name of my dataframe?

Also noticed that if i do data.frame(XX) it adds an X to all the column headers that have numbers at the front.

Henrik
  • 65,555
  • 14
  • 143
  • 159
Nathaniel Saxe
  • 1,527
  • 2
  • 15
  • 25

2 Answers2

32

Works for me

data.frame(`1`=rnorm(3), `2`=head(letters,3), check.names=FALSE)
#        1 2
# 1 0.5019 a
# 2 1.1148 b
# 3 0.4787 c
Marek
  • 49,472
  • 15
  • 99
  • 121
  • thanks. I understood about numbers, but it seems that some unicode text is disallowed. When I'm using a frame just for formatting, your suggestion comes in handy. – Steven R. Loomis Dec 16 '11 at 07:04
27

Yes, because R won't allow names of objects to start with numbers. If you were to call attach() with the data.frame, this would cause some issues.

data.frame (and read.table) function has the check.names parameter (default is TRUE)

If TRUE then the names of the variables in the data frame are checked to ensure that they are syntactically valid variable names and are not duplicated. If necessary they are adjusted (by make.names) so that they are.

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. [...] The character "X" is prepended if necessary.

Henrik
  • 65,555
  • 14
  • 143
  • 159
Shane
  • 98,550
  • 35
  • 224
  • 217