Speed trick: unlist(…, use.names=FALSE) is heaps faster!

January 7, 2013

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

Sometimes a minor change to your R code can make a big difference in processing time. Here is an example showing that if you’re don’t care about the names attribute when unlist():ing a list, specifying argument use.names=FALSE can speed up the processing lots!

> x <- split(sample(1000, size=1e6, rep=TRUE), rep(1:1e5, times=10))
> t1 <- system.time(y1 <- unlist(x))
> t2 <- system.time(y2 <- unlist(x, use.names=FALSE))
> stopifnot(identical(y2, unname(y1)))
> t1/t2
user system elapsed
103 NaN 104

That’s more than a 100 times speedup.

So, check your code to see to which unlist() statements you can add an use.names=FALSE.

To leave a comment for the author, please follow the link and comment on their blog: jottR.

R-bloggers.com 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)