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

[This article was first published on R – Win-Vector Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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

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

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
}
print(result)
# [[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 about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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)