Skip to Content

Create a Vector in R – With Examples

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?

create a vector in r using c()

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.

# 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.


Authors

  • Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.