Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data frame column naming

Tags:

dataframe

r

I am creating a simple data frame like this:

qcCtrl <- data.frame("2D6"="DNS00012345", "3A4"="DNS000013579")

My understanding is that the column names should be "2D6" and "3A4", but they are actually "X2D6" and "X3A4". Why are the X's being added and how do I make that stop?

like image 675
James Avatar asked Sep 12 '11 15:09

James


2 Answers

I do not recommend working with column names starting with numbers, but if you insist, use the check.names=FALSE argument of data.frame:

qcCtrl <- data.frame("2D6"="DNS00012345", "3A4"="DNS000013579", 
            check.names=FALSE)
qcCtrl

          2D6          3A4
1 DNS00012345 DNS000013579

One of the reasons I caution against this, is that the $ operator becomes more tricky to work with. For example, the following fails with an error:

> qcCtrl$2D6
Error: unexpected numeric constant in "qcCtrl$2"

To get round this, you have to enclose your column name in back-ticks whenever you work with it:

> qcCtrl$`2D6`
[1] DNS00012345
Levels: DNS00012345
like image 54
Andrie Avatar answered Nov 15 '22 12:11

Andrie


The X is being added because R does not like having a number as the first character of a column name. To turn this off, use as.character() to tell R that the column name of your data frame is a character vector.

like image 1
Stedy Avatar answered Nov 15 '22 12:11

Stedy