R Tip: Use vector(mode = “list”) to Pre-Allocate Lists

March 6, 2018

(This article was first published on R – Win-Vector Blog, and kindly contributed to R-bloggers)

Another R tip. Use vector(mode = "list") to pre-allocate lists.

result <- vector(mode = "list", 3)
#> [[1]]
#> [[2]]
#> [[3]]

The above used to be critical for writing performant R code (R seems to have greatly improved incremental list growth over the years). It remains a convenient thing to know.

Pre-allocation is particularly useful when using for-loops.

for(i in seq_along(result)) {
  result[[i]] <- i
# [[1]]
# [1] 1
# [[2]]
# [1] 2
# [[3]]
# [1] 3

seq_along() is a handy function similar to what we discussed in R Tip: Use seq_len() to Avoid The Backwards List Trap. For “[[ ]]” please see R Tip: Use [[ ]] Wherever You Can.

Note: for-loops are not in fact a always a bad thing (even in R). for-loops can be easier to debug, are the right solution when you are carrying state from iteration to iteration, and with proper pre-allocation can be as performant as map/apply methods. Mostly one should not use them where better vectorized operations can be used. For example: in R it is usually wrong to try to iterate over rows in a data.frame, as usually thare are vectorized operators that can more efficiently write the same process in terms of column operations.

To leave a comment for the author, please follow the link and comment on their blog: R – Win-Vector Blog.

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)