Numbers as column names of data frames

谁都会走 提交于 2019-11-27 14:39:55
Shane

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.

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!