Naming data column names

I am creating a simple data frame, for example:

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

I understand that the column names must be “2D6” and “3A4”, but they are actually “X2D6” and “X3A4”. Why is X being added and how to do it?

+6
source share
2 answers

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

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

One of the reasons I'm warning about this is because the $ operator becomes more complex to work with. For example, the following failure occurs with an error:

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

To get around this, you must attach your column name in the reverse tick when you work with it:

 > qcCtrl$`2D6` [1] DNS00012345 Levels: DNS00012345 
+8
source

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

+1
source

Source: https://habr.com/ru/post/897158/


All Articles