c() (Combine) (R)

c() is R's fundamental function for combining values into a vector. It concatenates its arguments into a single vector, coercing them to a common type if they differ.

R

c() (for 'combine' or 'concatenate') is one of the first functions every R user learns. c(1, 2, 3) builds a numeric vector; c("a", "b") a character vector. It flattens its arguments, so combining vectors with c() joins them end to end: c(c(1, 2), c(3, 4)) is c(1, 2, 3, 4).

If you mix types, c() coerces everything to the most general one so the result stays a single-type atomic vector: c(1, "a") becomes character, c(TRUE, 2) becomes numeric. This silent coercion is a common surprise — to keep mixed types, use list() instead.

You can name elements inline with c(x = 1, y = 2), creating a named vector you can index by name. c() is used constantly to construct the vectors that feed nearly every other R operation.

Syntax

v <- c(1, 2, 3)
named <- c(a = 1, b = 2)

Examples

R

nums <- c(1, 2, 3)
more <- c(nums, 4, 5)
print(more)
print(c(TRUE, 1, "x"))

Output:

[1] 1 2 3 4 5
[1] "TRUE" "1"    "x"   

Learn it in a lesson

See also

Back to the Dictionary