Computing on the Language

October 27, 2011

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

And now for something a bit more esoteric….

I recently wrote a function to deal with a strange problem. Writing the function ended up being a fun challenge related to computing on the R language itself.

Here’s the problem: Write a function that takes any number of R objects as arguments and returns a list whose names are derived from the names of the R objects.

Perhaps an example provides a better description. Suppose the function is called ‘makeList’. Then 

x <- 1
y <- 2
z <- "hello"
makeList(x, y, z)


list(x = 1, y = 2, z = "hello")

It originally seemed straightforward to me, but it turned out to be very much not straightforward. 

Note that a function like this is probably most useful during interactive sessions, as opposed to programming.

I challenge you to take a whirl at writing the function, you know, in all that spare time you have. I’ll provide my solution in a future post.

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.

Search R-bloggers


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)