c()
c(..., recursive = FALSE, USE.NAMES = TRUE) Returns:
vector or list · Added in v1.0 · Updated March 13, 2026 · Base Functions vectors base creation combining
The c() function (short for “combine” or “concatenate”) is the most fundamental function in R for creating vectors. Almost every R script uses c() to group values together into a single vector. Understanding how c() works is essential for effective R programming.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
... | objects | required | Values to combine. Can be vectors, lists, or individual values. |
recursive | logical | FALSE | If TRUE, recursively flattens lists into vectors. |
USE.NAMES | logical | TRUE | If TRUE and the arguments are named, result inherits those names. |
Return Value
When recursive = FALSE (default): returns a vector of the same type as the input (or the highest type in the coercion hierarchy: NULL < raw < logical < integer < double < complex < character)
When recursive = TRUE: returns a flattened vector, unlisting all nested structures
Type Coercion
R automatically coerces elements to the most flexible type:
# Numeric and character: all become character
c(1, 2, "three")
# [1] "1" "2" "three"
# Integer and numeric: all become numeric
c(1L, 2.5)
# [1] 1.0 2.5
# Mixed logical and numeric: logical TRUE/FALSE becomes 1/0
c(TRUE, 0, 1)
# [1] 1 0 1
Examples
Creating Basic Vectors
# Numeric vector
nums <- c(1, 2, 3, 4, 5)
nums
# [1] 1 2 3 4 5
# Character vector
words <- c("apple", "banana", "cherry")
words
# [1] "apple" "banana" "cherry"
# Logical vector
flags <- c(TRUE, FALSE, TRUE, TRUE)
flags
# [1] TRUE FALSE TRUE TRUE
Combining Vectors
# Concatenate two vectors
a <- c(1, 2, 3)
b <- c(4, 5, 6)
c(a, b)
# [1] 1 2 3 4 5 6
# Append single value to vector
c(a, 7)
# [1] 1 2 3 4 5 6 7
# Prepend value
c(0, a)
# [1] 0 1 2 3
Using recursive = TRUE
# List with nested vectors
my_list <- list(a = c(1, 2), b = c(3, 4))
# Default (recursive = FALSE) returns a list
c(my_list, recursive = FALSE)
# $a
# [1] 1 2
# $b
# [1] 3 4
# recursive = TRUE flattens to vector
c(my_list, recursive = TRUE)
# a1 a2 b1 b2
# 1 2 3 4
Named Vectors
# Automatic naming with USE.NAMES = TRUE (default)
c(a = 1, b = 2, c = 3)
# a b c
# 1 2 3
# Disable naming
c(a = 1, b = 2, c = 3, USE.NAMES = FALSE)
# [1] 1 2 3
Common Use Cases
Building Data Frame Columns
df <- data.frame(
name = c("Alice", "Bob", "Charlie"),
age = c(25, 30, 35),
score = c(85.5, 92.0, 78.5)
)
Vector Initialization
# Create empty vector
empty <- c()
# Pre-allocate with NA
initialized <- c(rep(NA, 10))
Combining Different Types (Caution!)
# This coerces everything to character
mixed <- c(1, "two", 3)
# [1] "1" "two" "3"
# Check the type
typeof(mixed)
# [1] "character"