The c() function in R is the easiest way to create and combine vectors. It’s a core part of how R handles data, and it’s something every R user should know.
If you are just getting started with R, make sure to take our Introduction to R Programming course to learn all the basics, including vectors, lists, data frames, and more. I also wrote an article on how to learn R if you are looking to create a learning plan.
(And if you're curious about the hard way of creating vectors, I created an FAQ at the end.)
What Is the c() Function in R?
The R c() function documentation tells us that c() combines values into a vector (so I think it’s safe to assume the “c” stands for “combine”). If you're unclear what that means, I'll show how in the examples below.
Before we move on, I should say, more specifically, the c() function takes any number of arguments and returns a vector containing all the values. It also automatically coerces values to a common data type (to the most flexible data type) if the data types are different, in which case that would be necessary.
Using c() to Combine Values
Our first example: You can use c() to create vectors by combining individual values or variables. You can see with these examples that c() works for different data types, like numeric, character, and logical.
numeric_vector <- c(10, 20, 30, 40)
character_vector <- c("apple", "banana", "cherry")
logical_vector <- c(TRUE, FALSE, TRUE)
Combining Existing Vectors with c()
You can also combine existing vectors using c(). This is helpful when you want to merge multiple data sources or results.
first_half <- c(1, 2, 3)
second_half <- c(4, 5, 6)
full_vector <- c(first_half, second_half)
Using c() with Named Elements
You can assign names to elements within a vector using c(). This is useful for labeling data.
fruit_counts <- c(apple = 5, banana = 3, cherry = 7)
c() with Different Data Types
When combining different data types, c() will convert all elements to the most flexible one. For example, combining numbers and characters will result in a character vector. This is because "two" can't be read as a numeric type, but "1" could be a character.
mixed_vector <- c(1, "two", 3)
Our result here will be a character vector: "1", "two", "3".
Creating a Data Frame with c()
You can use c() to create vectors that will serve as your columns in a data frame. To do this, I'm also using the data.frame() function to put the vectors together.
customer_names <- c("Alice", "Bob", "Charlie")
purchase_totals <- c(120.50, 75.00, 99.99)
customer_data <- data.frame(name = customer_names, total = purchase_totals)
Conclusion
As you've seen, the c() function is an essential part of R programming. I showed in the examples how you can both create and combine vectors.
Take our Introduction to R Programming course for a structured learning path to start and keep advancing with your skills.
The
c()function in R is the easiest way to create and combine vectors. It’s a core part of how R handles data, and it’s something every R user should know.If you are just getting started with R, make sure to take our Introduction to R Programming course to learn all the basics, including vectors, lists, data frames, and more. I also wrote an article on how to learn R if you are looking to create a learning plan.
(And if you're curious about the hard way of creating vectors, I created an FAQ at the end.)
What Is the c() Function in R?
The R c() function documentation tells us that
c()combines values into a vector (so I think it’s safe to assume the “c” stands for “combine”). If you're unclear what that means, I'll show how in the examples below.Before we move on, I should say, more specifically, the
c()function takes any number of arguments and returns a vector containing all the values. It also automatically coerces values to a common data type (to the most flexible data type) if the data types are different, in which case that would be necessary.Using c() to Combine Values
Our first example: You can use
c()to create vectors by combining individual values or variables. You can see with these examples thatc()works for different data types, like numeric, character, and logical.Combining Existing Vectors with c()
You can also combine existing vectors using
c(). This is helpful when you want to merge multiple data sources or results.Using c() with Named Elements
You can assign names to elements within a vector using
c(). This is useful for labeling data.c() with Different Data Types
When combining different data types,
c()will convert all elements to the most flexible one. For example, combining numbers and characters will result in a character vector. This is because "two" can't be read as a numeric type, but "1" could be a character.Our result here will be a character vector: "1", "two", "3".
Creating a Data Frame with c()
You can use
c()to create vectors that will serve as your columns in a data frame. To do this, I'm also using thedata.frame()function to put the vectors together.Conclusion
As you've seen, the
c()function is an essential part of R programming. I showed in the examples how you can both create and combine vectors.Take our Introduction to R Programming course for a structured learning path to start and keep advancing with your skills.