Concatenating vectors, append() vs c()

Viewed 270

When concatenating two vectors, a and b, in R, it seems to me that

append(a,b)

and

c(a,b)

produces the same result. Are there any cases where one of the functions should be preferred over the other? Is append() meant for operations on lists rather than vectors?

1 Answers

Take a look at the append() function. Basically it is the addition of the after argument that sets it apart. In general, c() will be more efficient since it skips this little bit of logic.

function (x, values, after = length(x)) 
{
    lengx <- length(x)
    if (!after) 
        c(values, x)
    else if (after >= lengx) 
        c(x, values)
    else c(x[1L:after], values, x[(after + 1L):lengx])
}
Related