Computing on the Language Followup

November 1, 2011

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

My article on computing on the language was unexpectedly popular and so I wanted to quickly follow up on my own solution. Many of you got the answer, and in fact many got solutions that were quite a bit shorter than mine. Here’s how I did it:

makeList <- function(...) {
        args <- substitute(list(...))
        nms <- sapply(args[-1], deparse)
        vals <- list(...)
        names(vals) <- nms

Baptiste pointed out that Frank Harrell has already implemented this function in his Hmisc package as the ‘llist’ function (thanks for the pointer!). I’ll just note that this function does a bit more actually because each element of the returned list is an object of class “labelled”.

The shortest solution was probably Tony Breyal’s version:

makeList <- function(...) {
        structure(list(...), names = names(data.frame(...)))

However, it’s worth noting that this function modifies the object’s name if the name is non-standard (i.e. if you’re using backticks like `r object name`). That’s just because the ‘data.frame’ function automatically modifies names if they are non-standard.

Thanks to everyone for the responses! I’ll try to come up with another one soon.

To leave a comment for the author, please follow the link and comment on their blog: Simply Statistics. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...


Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)