Reputation: 59455
My dataframe looks like this:
sum <- data.frame(
"default LS" = rnorm(3),
"fit LS" = rnorm(3),
"gradient dMNLL/dLS" = rnorm(3)
)
Now, the spaces got converted to .
(dots), for syntax reasons:
default.LS fit.LS gradient.dMNLL.dLS
1 0.1157615 0.06711939 1.5897061
2 1.1819154 1.11368192 -0.1730422
3 0.1531863 -0.63845188 0.6946397
I don't mind in the code, but for presentation purposes, I would like to print it with spaces.
Is there a way to print the data.frame with spaces?
Is there a way to print data.frame with custom column names?
Upvotes: 0
Views: 298
Reputation: 59455
Posting the solution mentioned by @Roland also for others as an answer:
Use check.names = FALSE
when creating the data.frame
:
sum <- data.frame(
"default LS" = rnorm(3),
"fit LS" = rnorm(3),
"gradient dMNLL/dLS" = rnorm(3),
check.names = FALSE
)
But that makes working with this data.frame a nuisance because you need syntax like sum$"fit LS"
. Better to do:
names(sum) <- gsub(".", " ", names(sum), fixed = TRUE)
right before printing.
Upvotes: 1
Reputation: 1228
You can also use tibble objects from the tidyverse
packages.
library(tibble)
data <- tibble("Variable with space"=letters)
data
Even though it has ticks around the variable name when printed directly, it doesn't stick around for other printing situations (like plots or tables).
Upvotes: 1
Reputation: 1803
sum <- data.frame(
"default LS" = rnorm(3),
"fit LS" = rnorm(3),
"gradient dMNLL/dLS" = rnorm(3)
)
names(sum)<- gsub("\\."," ", make.names(names(sum), unique = T))
sum
#> default LS fit LS gradient dMNLL dLS
#> 1 -0.7607631 -0.6488190 -2.1846567
#> 2 1.3633617 -0.1891804 1.0127126
#> 3 0.1476440 0.6775571 -0.7808632
sum[["default LS"]]
#> [1] -0.7607631 1.3633617 0.1476440
To select a particular column, the column name passed in ""
or ``
Upvotes: 0