0
0
R Programmingprogramming~5 mins

Vector creation with c() in R Programming

Choose your learning style9 modes available
Introduction

The c() function in R is used to combine values into a single vector. It helps you group data together in one place.

When you want to create a list of numbers to work with.
When you need to combine several values into one variable.
When you want to store multiple related items like names or scores.
When preparing data for calculations or graphs.
When you want to easily access a group of values by their position.
Syntax
R Programming
c(value1, value2, value3, ...)

You can mix numbers, text, or logical values inside c().

The values inside c() become elements of a vector.

Examples
This creates a numeric vector with five numbers.
R Programming
numbers <- c(1, 2, 3, 4, 5)
This creates a character vector with three letters.
R Programming
letters <- c("a", "b", "c")
This creates a logical vector with TRUE and FALSE values.
R Programming
mixed <- c(TRUE, FALSE, TRUE)
This creates an empty vector with no elements.
R Programming
empty <- c()
Sample Program

This program shows how to create numeric and character vectors using c() and then prints them.

R Programming
# Create a numeric vector
my_numbers <- c(10, 20, 30, 40)

# Print the vector
print(my_numbers)

# Create a character vector
my_letters <- c("x", "y", "z")

# Print the character vector
print(my_letters)
OutputSuccess
Important Notes

If you mix different types, R will convert them all to the most flexible type (usually character).

Vectors created with c() are one-dimensional and can be used for many basic tasks in R.

Summary

c() combines values into a vector.

Vectors can hold numbers, text, or logical values.

Use c() to group related data for easy use.