Skip to main content

R c Function(): Creating Vectors the Easy Way

Learn how to use the c() function in R to combine values into vectors efficiently.
Jul 1, 2025  · 2 min read

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.


Josef Waples's photo
Author
Josef Waples

I'm a data science writer and editor with contributions to research articles in scientific journals. I'm especially interested in linear algebra, statistics, R, and the like. I also play a fair amount of chess! 

Final Question

You said c() is the easy way to create vectors. What’s the hard way?

While c() is the most convenient and beginner-friendly method, there are more complex ways to create vectors in R. Here are a few:

Manual Initialization with vector() and Indexing 

You can create an empty vector of a certain type and length, then fill it one element at a time:

x <- vector("numeric", 3) x[1] <- 1 x[2] <- 2 x[3] <- 3

This works but is more verbose and less intuitive for beginners.

Using append() Repeatedly 

You could start with an empty vector and keep appending values:

x <- c() x <- append(x, 1) x <- append(x, 2) x <- append(x, 3)

This is inefficient and harder to read.

Using Other Functions like seq() or rep()

These functions generate specific patterns and are powerful—but overkill for simple vectors:

x <- seq(1, 3) y <- rep(1:3, times = 1)

 Flattening Other Structures 

You could extract elements from a list and convert it to a vector:

my_list <- list(1, 2, 3) x <- unlist(my_list)
Topics

Learn R with DataCamp

Course

Introduction to R

4 hr
2.9M
Master the basics of data analysis in R, including vectors, lists, and data frames, and practice R with real data sets.
See DetailsRight Arrow
Start Course
See MoreRight Arrow
Related

Tutorial

Scatterplot in R

Learn how to create a scatterplot in R. The basic function is plot(x, y), where x and y are numeric vectors denoting the (x,y) points to plot.
DataCamp Team's photo

DataCamp Team

Tutorial

Using Functions in R Tutorial

Discover what R functions are, the different type of functions in R, and how to create your own functions in R.
Javier Canales Luna's photo

Javier Canales Luna

11 min

Tutorial

Arrays in R

Learn about Arrays in R, including indexing with examples, along with the creation and addition of matrices and the apply() function.

Olivia Smith

8 min

Tutorial

Operators in R

Learn how to use arithmetic and logical operators in R. These binary operators work on vectors, matrices, and scalars.
DataCamp Team's photo

DataCamp Team

4 min

Tutorial

R Formula Tutorial

Discover the R formula and how you can use it in modeling- and graphical functions of well-known packages such as stats, and ggplot2.
Karlijn Willems's photo

Karlijn Willems

15 min

Tutorial

Combining Plots

Learn how to combining multiple plots in R into one graph with either the par() or layout() functions. This page includes coding examples.
DataCamp Team's photo

DataCamp Team

4 min

See MoreSee More