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 name? ie, "Label 1" instead of "Label.1".
You can change an existing data frames names to contain spaces ie using your example
returns
and you can still access the columns using the $ operator, you just need to use double quotes eg
returns
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.
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 themake.names()
function for details or an example:You may set
check.names = FALSE
indata.frame
(as well as inread.table
):returns:
From
?data.frame
:From
?make.names
:Also, if you need to subset a variable with an 'invalid' name using
$
, you can use backticks`
. For example: