If you surround the "column name" (different than a true R name) with back-ticks you can get away with accessing columns containing special characters that would otherwise trip up the parser. This means you will need to use those backticks every time you want to refer to that column name. However, the data.frame
function won't let you do this unless you explicitly tell it to avoid sanitizing the column names:
> dfmine <- data.frame(`weird (column) name with %'s and other stuff`= 1:10)
> dfmine
weird..column..name.with...s.and.other.stuff
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
Failed to set the correct parameter, check.names
. Now do it "correctly":
> dfmine <- data.frame(`weird (column) name with %'s and other stuff`= 1:10,
check.names=FALSE)
> dfmine$`weird (column) name with %'s and other stuff`
[1] 1 2 3 4 5 6 7 8 9 10
> dfmine
weird (column) name with %'s and other stuff
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
This is generally not a good strategy and there are usually other methods to accomplish whatever it might be that leads you in the direction of this strategy. The other way to get to that point would be to assign a valid name and then reassign using an ordinary character value:
> dfmine <- data.frame(`weird (column) name with %'s and other stuff`= 1:10)
> colnames(dfmine) <- "weird (column) name with %'s and other stuff"
> dfmine
weird (column) name with %'s and other stuff
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10