poplitea
poplitea

Reputation: 3737

Concatenating vectors, append() vs c()

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?

Upvotes: 5

Views: 645

Answers (1)

user10917479
user10917479

Reputation:

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])
}

Upvotes: 8

Related Questions