Computing on the Language

October 27, 2011
By

(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)

returns

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