Fast Append in R – Quickly Add Elements To A List

Modern computers are fast enough that you rarely need to worry about performance. That being said, certain statistical methods require fast calculations. This is very common for forecasting, statistical modeling, and data interpolation. R’s append() function offers a quick way to accomplish this for lists and vectors, faster than the default concatenation approach.

It should be noted that you don’t *usually* have to do this. The performance improvement for small vectors is likely negligible. But if you’re cranking out a couple hundred thousand values, this helps.

Meet the Append() function

The basic syntax of an append operation is simple.

append (first_vector, second_vector)

You are likely already familiar with using concatenate to add elements to a list. While this does a solid job of adding items to a list in R, the append function operates faster.

Append also allows you to specify where to append the values within the list or vector. This will add the items after the named element. For example, the following code will add the new values after the fifth element of the original list.

 append (first_vector, second_vector, after=5) 

You also can directly specifying a list or vector as a source in the append statement.


append (first_vector, c(value1, value2, value3), after=5) 

This approach makes for more succinct code.