James
James

Reputation: 1457

Data frame column naming

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?

Upvotes: 6

Views: 2572

Answers (2)

Andrie
Andrie

Reputation: 179398

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

Upvotes: 8

Stedy
Stedy

Reputation: 7469

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.

Upvotes: 1

Related Questions