How to Combine Vectors in R with c() and append()
Combine vectors with c(), the standard function for concatenating elements in R. The result preserves input order: all elements from the first vector appear before all elements from the second. Both vectors must be compatible types; if they differ, R coerces to the broader type — mixing integers and doubles produces a double vector, and mixing numbers with characters produces a character vector. When you need to insert elements at a specific position instead of always appending at the end, use append() with the after argument to control where the new elements land.
x <- c(1, 2, 3)
y <- c(4, 5, 6)
c(x, y)
# [1] 1 2 3 4 5 6
c() works for any atomic vector type: logical, integer, double, or character. When you need to insert elements at a specific position rather than always appending at the end, use append():
append(x, y, after = 2)
# [1] 1 2 4 5 6 3
Set after = 0 to prepend. Both c() and append() return a new vector — neither modifies the original vectors in place. That is the expected behavior in R, but it means you should assign the result if you want to keep it.