R and the last comma

R-bloggers 2013-04-01

(This article was first published on R de jeu, and kindly contributed to R-bloggers)

In R, every comma matters. When creating a vector, c(1, 2, 5) will do the right thing, but add one unfortunate comma and c(1, 2, 5,) will greet you with a deadly Error in c(1, 2, 5, ) : argument 4 is empty.

Other languages like Perl are less strict when defining basic data structures: having a comma after the last item is allowed. This can be particularly useful when items are specified on multiple lines as in this example:

my @cities = (   "New York",   "Washington",   "Atlanta", )

Because the last line item is syntactically no different than any other, I can comment, uncomment, add, remove, swap items anywhere within the definition without having to worry about which lines should have a comma or not (they all should.)

Can R's default behavior be overridden? Yes, using the following functional:

The functional acts as a wrapper around any function: it creates an identical function but such that the last argument, if missing, is thrown out.

This way, I can call functions like c, list, data.frame indirectly and with an optional extra comma at the end:

cities   "New York",   "Washington",   "Atlanta", )

I can even put the definition of ok.comma into my .Rprofile file and redefine functions

c          list       data.frame

so I can seamlessly do:

cities   "New York",   "Washington",   "Atlanta", )

I hope you find this useful.

To leave a comment for the author, please follow the link and comment on his blog: R de jeu.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series,ecdf, trading) and more...