It’s testing week in the Software Carpentry course. To commemorate the occasion, I’ve uploaded a presentation I gave a couple of years ago to evangelise the use of testing frameworks to make your life easier. Just for you, I’ve recorded an all new audio track to accompany the slides. The example uses MATLAB but the principle is applicable to any language.
The help page to
make.names describes how to make a valid variable name in R:
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.
What it doesn’t tell you is that you can generate invalid variable names and still use them perfectly well, with a bit of trickery. To create and use such “invalid” variables, you enclose their name in backquotes.
`an invalid variable` <- 69 `an invalid variable`
You can get even more pathological, if your mind is suitably fiendish.
`FALSE` <- TRUE `...` <- "ellipsis"
The last example illustrates a danger of using these silly names. Printing
`...` throws an error, though you can retrieve the contents of the variable using
The main use of these silly names is for writing obfuscated code, so the first-person shooter you’re writing in R looks like just another routine data analysis to your boss. However, I did recently find a use for what is arguably the stupidest possible variable name: a single space.
Sometimes when you view a CSV file, it is easier to see things when you have a bit of white space. Adding blank columns can provide such utility. The trouble is, since these columns have no real use from a data analysis perspective,
write.csv doesn’t give you the opportunity to create blank columns willy-nilly. Here’s a little hack to do such a thing. In your data frame, include a column with name
` ` and value
dfr <- data.frame(x = 1:5, ` ` = NA, y = runif(5), check.names = FALSE) write.csv(dfr, "blank column.csv", na = "")
Is this easier than right-clicking the column header in the CSV file and selecting Insert? Probably not, but doing things in code is always more satisfying.