Home > R > A legitimate use for the stupidest variable name ever

A legitimate use for the stupidest variable name ever

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 get("...").

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 NA.

dfr <- data.frame(x = 1:5, ` ` = NA, y = runif(5), check.names = FALSE)
write.csv(dfr, "blank column.csv", na = "")

the CSV file contains a blank column

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.

Advertisements
Tags: , ,
  1. Ethan Brown
    2nd February, 2011 at 16:06 pm

    How devious!

    I love that this comes in R bloggers immediately after a post on literate programming. πŸ™‚

  2. Ellbur
    4th July, 2011 at 15:57 pm

    This is awesome. But what if you wanted *two* blank columns? Would you have to use different numbers of spaces for each?
    Or with unicode I guess you could use different kinds of spaces.

    • 4th July, 2011 at 16:19 pm

      Since you aren’t checking column names when you create the data frame, it’s okay to have two or more columns, all named ” “.

      dfr <- data.frame(x = 1:5, ` ` = NA, ` ` = NA, y = runif(5), check.names = FALSE)

  1. 4th July, 2011 at 0:03 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: