数字作为数据框的列名称 [英] Numbers as column names of data frames
问题描述
R是否不允许我使用数字作为数据框的列名?
Is there a reason why R won't allow me to have a number as the column name of my dataframe?
还注意到,如果我执行 data.frame(XX)
,则会添加 X
到所有前面带有数字的列标题。
Also noticed that if i do data.frame(XX)
it adds an X
to all the column headers that have numbers at the front.
推荐答案
是的,因为R不会t允许对象名称以数字开头。如果要使用data.frame调用 attach()
,这会引起一些问题。
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
(和 read.table
)函数具有 check.names
参数(默认为 TRUE
)
data.frame
(and read.table
) function has the check.names
parameter (default is TRUE
)
如果
如果为TRUE
,则将检查数据框中变量的名称,以确保它们在语法上是有效的变量名称,并且没有重复。如有必要,可以对它们进行调整(通过make.names
进行调整)。
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 (bymake.names
) so that they are.
来自?make.names
:
语法上有效的名称由字母组成,数字和点或下划线字符,并以字母或点开头,后跟数字。 [...]如有必要,使用字符
X
。
这篇关于数字作为数据框的列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!