Reputation: 6542
What is the difference between using c()
and append()
? Is there any?
> c( rep(0,5), rep(3,2) )
[1] 0 0 0 0 0 3 3
> append( rep(0,5), rep(3,2) )
[1] 0 0 0 0 0 3 3
Upvotes: 47
Views: 47345
Reputation: 118839
The way you've used it doesn't show difference between c
and append
. append
is different in the sense that it allows for values to be inserted into a vector after a certain position.
Example:
x <- c(10,8,20)
c(x, 6) # always adds to the end
# [1] 10 8 20 6
append(x, 6, after = 2)
# [1] 10 8 6 20
If you type append
in R terminal, you'll see that it uses c()
to append values.
# append function
function (x, values, after = length(x))
{
lengx <- length(x)
if (!after)
c(values, x)
# by default after = length(x) which just calls a c(x, values)
else if (after >= lengx)
c(x, values)
else c(x[1L:after], values, x[(after + 1L):lengx])
}
You can see (the commented part) that by default (if you don't set after=
as in your example), it simply returns c(x, values)
. c
is a more generic function that can concatenate values to vectors
or lists
.
Upvotes: 59