R is a powerful programming language used for statistical analysis and applications. It supports six basic data structures – vector, list, matrix, array, factor, and dataframe to store, analyze and handle data in an easy way. In this tutorial, we will look at how to create a vector in R with the help of some examples.
What is a vector in R?
A vector in the R programming language is a data structure used to store one-dimensional data of the same type. For example, a vector of numbers, a vector of characters, etc. The values in a vector are ordered and are indexed starting from 1.
How to create a vector in R?

You can use the combine function, c() to create a vector in R. Pass the values you want to include in the vector as arguments. The following is the syntax –
# create a vector in R vec <- c(val1, val2, val3, ...)
Here, the resulting vector from the c()
method above is stored in the variable vec
. It is important to keep the following points in mind when working with vectors in R –
- The values in the a vector are of the same data type. If you pass values of different data types, the values are coerced into the data type with the highest priority.
- The priority order for different data types is – logical < numeric < complex < characters.
Examples
Let’s look at some examples of creating a vector in R.
Vector of integers in R
To create a vector of integers, pass the integers to the c()
function in the order you want them in the vector. Let’s look at an example.
# create a vector of integers in R vec <- c(1, 2, 3, 4, 5) # display the vector print(vec)
Output:
[1] 1 2 3 4 5
You can see that the resulting vector contains integers. You can also get the type of values in a vector in R using the class()
method.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
# display the type of vector class(vec)
Output:
'numeric'
You can see that the values in the above vector are “numeric”.
If you want to create a vector of contiguous integers, for example, 1 to 5, you can use the :
symbol instead.
# create a vector of contiguous integers vec <- 1:5 # display the vector print(vec)
Output:
[1] 1 2 3 4 5
We get a vector of contiguous integers from 1 to 5.
Vector of real numbers in R
Let’s now create a vector of real numbers. Note that both integers and real numbers are represented with the “numeric” type in R.
# create a vector of real numbers in R vec <- c(1.5, 3.14, 2.71) # display the vector print(vec)
Output:
[1] 1.50 3.14 2.71
Here, we create a vector of three real numbers. Let’s print the type of values in this vector.
# display the type of vector class(vec)
Output:
'numeric'
We get “numeric” as the type.
Vector of strings in R
Strings are represented with the “character” type in R. Let’s create a vector with only string values.
# create a vector of characters in R vec <- c("a", "b", "cat") # display the vector print(vec)
Output:
[1] "a" "b" "cat"
Let’s now get the type of the values in this vector.
# display the type of vector class(vec)
Output:
'character'
As expected, we get “character” as the data type.
Vector of logical values in R
You can similarly create a vector with only logical values – TRUE
and FALSE
.
# create a vector of logical values in R vec <- c(T, F, T, T) # display the vector print(vec)
Output:
[1] TRUE FALSE TRUE TRUE
Note that you can use T
to represent TRUE
and F
to represent FALSE
in R. Let’s confirm the type of the values in this vector.
# display the type of vector class(vec)
Output:
'logical'
Vector with values from different data types in R
What would happen if you try to create a vector with mixed values? That is values from different data types, like numeric, character, and logical. Let’s find out.
# vector with mixed values in R vec <- c(1, "a", TRUE) # display the vector print(vec)
Output:
[1] "1" "a" "TRUE"
Here, we pass a numeric, a character, and a logical type value to the c()
function. Let’s get the type of the resulting vector.
# display the type of vector class(vec)
Output:
'character'
The values in the resulting vector are of the “character” type. This is because a vector can store only values of the same type and therefore R performs automatic coercion so that the values are consistent.
The following is the priority order of different data types. R does internal coercion such that the values are consistent with the simplest data type.
logical < numeric < complex < characters
Let’s look at another example. Here, we will pass numeric and logical values to the c()
function.
# vector with mixed values in R vec <- c(1, 3, 5, TRUE, FALSE) # display the vector print(vec)
Output:
[1] 1 3 5 1 0
You can see that the resulting vector is of the “numeric” type. Logical values TRUE
and FALSE
are converted to 1 and 0 respectively.
You might also be interested in –
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.