Computing on the Language Followup

November 1, 2011
By

(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
        vals
} 

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 his blog: Simply Statistics.

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

Tags:

Comments are closed.