← Back to R Course | Chapter 3: Vectors | Lesson 2 of 7

The c() Function

In this page:

  1. The c() Function

The c() Function

c() (combine) builds a vector out of individual values or merges existing vectors together. If the values have mixed types, R coerces them all to the most general common type, such as converting numbers to strings if any element is a character. This coercion rule is important to keep in mind when combining data from different sources.

Warning: c(1, 2, "three") coerces every element to character -- mixing types silently promotes the whole vector to the most general type.

Example: The c() Function

markup
numbers <- c(1, 2, 3)
combined <- c(numbers, 4, 5)
print(combined)

mixed <- c(1, 2, "three")
print(mixed)
cat(class(mixed), "\n")
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.