Specifying column names in a data.frame changes spaces to “.”

帅比萌擦擦* 提交于 2019-11-27 09:11:24

You don't.

With the space you desire the format would not satisfy the requirements for an identifier that come to play when you use df$column.1 -- that could not cope with a space. So see the make.names() function for details or an example:

> make.names(c("Foo Bar", "tic tac"))
[1] "Foo.Bar" "tic.tac"  
>                                              
Brandon Bertelsen

You may set check.names = FALSE in data.frame (as well as in read.table):

df <- data.frame("Label 1" = 1:3, "Label 2" = rnorm(3), check.names = FALSE)

returns:

  Label 1    Label 2
1       1  0.2013347
2       2  1.8823111
3       3 -0.5233811

From ?data.frame:

check.names
logical. 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. Names such as ".2way" are not valid, and neither are the reserved words.

All invalid characters are translated to "."


Also, if you need to subset a variable with an 'invalid' name using $, you can use backticks `. For example:

df$`Label 1`

You can change an existing data frames names to contain spaces ie using your example

x <- c(1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10)
df <- data.frame("Label 1"=x,"Label 2"=rnorm(100))
colnames(df) <- c("Label 1", "Label 2")
head(df, 3)

returns

  Label 1    Label 2
1       1  0.2013347
2       2  1.8823111
3       3 -0.5233811

and you can still access the columns using the $ operator, you just need to use double quotes eg

df$"Label 2"[1:3]

returns

[1]  0.2013347  1.8823111 -0.5233811

It seems rather inconsistent to me to auto-convert column names upon data.frame creation, but not to-do the same during column name alteration, but thats how R works at the moment.

Tanmay
names(df)<-c('Label 1','Label 2)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!