tags:

views:

63

answers:

3

Let's say I have a data.frame, like so:

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))

head(df,3)

returns:

  Label.1    Label.2
1       1  1.9825458
2       2 -0.4515584
3       3  0.6397516

How do I get R to stop automagically replacing the space with a period in the column label? ie, "Label 1" instead of "Label.1".

+3  A: 

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"  
>                                              
Dirk Eddelbuettel
Hrmmm, this is for output purposes. The data.frame will not be used for further calculations at this point (ie, it's going straight to write.table())
Brandon Bertelsen
It's a language requirement. You can create your own pretty printing functions that do the substitution *for output* but you cannot change the way the data.frame is created.
Dirk Eddelbuettel
@Brandon, you can specify `col.names` in `write.table`. Something like `col.names=gsub("\\."," ",colnames(df))` should do the trick.
Joshua Ulrich
data.frame(..., check.names=F) worked.
Brandon Bertelsen
Agree with the above comments. If it's for formatted output, then specify the space as part of the output process. Spaces in identifiers is just asking for trouble which is why they are discouraged/disallowed.
neilfws
+2  A: 

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.

Aaron Statham
+3  A: 

I found a solution as well:

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), check.names=F)

returns:

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

Adding check.names=F argument worked out.

Brandon Bertelsen