Vectors in R

In this tutorial you will learn about vectors, type of vectors, vector operations, accessing elements of vectors and coercion of vectors in R.

Vectors in R

A basic data structure in R is vector. It is a atomic (homogeneous) data structure, where all the stored objects are of same type. Vector can be defined as a set of constants or scalars of same type arranged in a one-dimensional array.

vectors in R
vectors in R

In R there are four type of vectors, namely,

  • numeric vector (integer or double)
  • character vector
  • logical vector
  • complex vector

R provides a set of explicit function to initialize a vector, to check the class of a vector and to coerce a vector. The list of vector class functions are as follows:

Initialize Check the class Coerce function
integer() is.integer() as.integer()
numeric() is.numeric() as.numeric()
character() is.character() as.character()
logical() is.logical() as.logical()
complex() is.complex() as.complex()

Some important properties of vectors can be checked using the following functions. Knowledge about these properties are very important while manipulating vectors.

Property Command Meaning
Type typeof() Type of Storage mode of object
Length length() Number of elements in object
Class class() Class of object
Mode mode() Storage mode of object
Structure str() Compactly display the structure of object

The str() function check the internal structure of the R object and print a preview of its content.

Vectors are usually created with c() function. The c() function is known as concatenation or combine which combines its arguments.

Initialize a vector in R

Below R code initialize vec.int as a integer vector of length 5.

vec.int <- integer(5)
vec.int
[1] 0 0 0 0 0
class(vec.int)
[1] "integer"

Below R code initialize vec.num as a numeric vector of length 4.

vec.num <- numeric(4)
vec.num
[1] 0 0 0 0
class(vec.num)
[1] "numeric"

Below R code initialize a logical vector of length 4.

vec.log <- logical(4)
vec.log
[1] FALSE FALSE FALSE FALSE
class(vec.log)
[1] "logical"

Below R code initialize a character vector of length 5.

vec.char <- character(5)
vec.char
[1] "" "" "" "" ""
class(vec.char)
[1] "character"

Below R code initialize a complex vector of length 4.

vec.comp <- complex(4)
vec.comp
[1] 0+0i 0+0i 0+0i 0+0i
class(vec.comp)
[1] "complex"

Creating an empty vector

An empty vector can be created with the vector() function. The function vector(mode,length) produces a vector of the given mode and length.

Below R code create an empty numeric vector of length 3.

vec1 <- vector(mode = "numeric", length = 3)
vec1
[1] 0 0 0

Below R code create an empty logical vector of length 4.

vec2 <- vector(mode = "logical", length = 4)
vec2
[1] FALSE FALSE FALSE FALSE

Below R code create an empty complex vector of length 3.

vec3 <- vector(mode = "complex", length = 3)
vec3
[1] 0+0i 0+0i 0+0i

Below R code create an empty character vector of length 5.

vec4 <- vector(mode = "character", length = 5)
vec4
[1] "" "" "" "" ""

Numeric Vector

Numeric vectors have elements which are numeric (integer or double) values. If the numbers are follows by letter L then R define the vector as an integer vector otherwise as a double vector.

Suppose we have age of 5 individuals as 20,31,45,37 and 24. Below R code store the age of 5 individuals as integer to a integer vector age.1.

# store the values as integer to age.1
age.1 <- c(20L, 31L, 45L, 37L, 24L)
age.1
[1] 20 31 45 37 24

In the above code first line create an object age.1, as a integer vector, containing the entries 20, 31, 45, 37, 24. The second line display the output of age.1.

Below R code display the structure of age.1. It is clear from the output that the vector age.1 is an integer vector with 5 elements.

# display structure
str(age.1) 
 int [1:5] 20 31 45 37 24
# display length
length(age.1)  
[1] 5
# display mode
mode(age.1)  
[1] "numeric"
# display storage mode
typeof(age.1) 
[1] "integer"
is.vector(age.1)
[1] TRUE
is.vector(age.1, mode = "logical")
[1] FALSE

Below R code create an object age.2 as a vector of continuous values (double).

# Store the values as double to age.2
age.2 <- c(20, 31, 45, 37, 24)
age.2
[1] 20 31 45 37 24

Remember that, while defining age.1 we have used integer numbers follows by L. Hence, R create age.1 as a integer vector while age.2 as a double vector.

Below R code display the structure of vector age.2. It is clear from the output that the age.2 vector is a numeric vector with 5 elements.

# display structure
str(age.2)
 num [1:5] 20 31 45 37 24
# display mode
mode(age.2)   
[1] "numeric"
# display storage mode
typeof(age.2) 
[1] "double"

We can see the difference between the results of typeof() command for age.1 and age.2. The typeof(age.1) display the output integer while typeof(age.2) display the output double.

Character Vectors

Character vectors have elements which are character strings, where strings are sequence of characters enclosed in double quotes, "First", or single quotes, 'First'. The function nchar() returns the length of the character strings in a character vector.

result<-c("First","Second","Third",
          "Pass","Fail","Second","Pass")
result
[1] "First"  "Second" "Third"  "Pass"   "Fail"   "Second" "Pass"  

Below R code display the structure of result. It is clear from the output that the result vector is character vector with 7 elements.

# display structure
str(result)
 chr [1:7] "First" "Second" "Third" "Pass" "Fail" "Second" "Pass"
# Display the length of the each character string
nchar(result) 
[1] 5 6 5 4 4 6 4
# Display the number of element in result
length(result) 
[1] 7
# Display the mode of data stored in result
mode(result) 
[1] "character"
typeof(result) 
[1] "character"

Logical Vectors

Logical vectors have elements which are logical character strings T or TRUE and F or FALSE.

x <- c(T, F, T, F, T, F)
x
[1]  TRUE FALSE  TRUE FALSE  TRUE FALSE
str(x)
 logi [1:6] TRUE FALSE TRUE FALSE TRUE FALSE
mode(x)
[1] "logical"
typeof(x)
[1] "logical"
y <- c(TRUE, FALSE, TRUE, TRUE)
length(y)
[1] 4
mode(y)
[1] "logical"
typeof(y)
[1] "logical"

Logical vectors can also be created using a logical expression on numeric vector.

a <- c(1, 2, 3, 5)
mode(a)
[1] "numeric"
z <- a < 2.5 # logical expression
z
[1]  TRUE  TRUE FALSE FALSE
mode(z)
[1] "logical"

Complex Vectors

Complex vectors have elements which are complex constants.

z1 <- c(1i, 0i, 2 + 3i, 3 - 4i)
z1
[1] 0+1i 0+0i 2+3i 3-4i
str(z1)
 cplx [1:4] 0+1i 0+0i 2+3i ...
z2 <- c(0, 0, 1 + 3i, -4i)
z2
[1] 0+0i 0+0i 1+3i 0-4i
str(z2)
 cplx [1:4] 0+0i 0+0i 1+3i ...
length(z2)
[1] 4
mode(z2)
[1] "complex"
typeof(z2)
[1] "complex"

Some functions associated with complex vectors are as follows:

Re(z2) # gives real part of z2
[1] 0 0 1 0
Im(z2) # gives imaginary part of z2
[1]  0  0  3 -4
Mod(z2) # gives modulus of z2
[1] 0.000000 0.000000 3.162278 4.000000
Conj(z2) #  gives conjugate of z2
[1] 0+0i 0+0i 1-3i 0+4i

Creating Vector with named elements

Suppose you want to create a named vector with weight of students along with their names. One way is to create a Weight vector as follows:

Weight <- c(58, 46, 75, 49, 67)
Weight
[1] 58 46 75 49 67

Then use names() function to assign name of students.

names(Weight) <- c("Jatin", "Shruti", "David", "Parag", "Daniel")
Weight
 Jatin Shruti  David  Parag Daniel 
    58     46     75     49     67 

Another way to create a named vector using a single command is as follows:

Weight <- c(
  "Jatin" = 58,
  "Shruti" = 46,
  "David" = 75,
  "Parag" = 49,
  "Daniel" = 67
)
Weight
 Jatin Shruti  David  Parag Daniel 
    58     46     75     49     67 

Vectors using functions

Generating vectors using : operator

R provides a very useful way of generating a regular sequence of increasing or decreasing values using sequence operator :.

The general expression is n1:n2, which generates sequence of integers ranging from n1 to n2.

It generates a sequence of values n1, n1 + 1, ... , up to the sequence value less than or equal to n2.

Examples of : operator

# generate sequence of numbers from 2 to 6
x <- 2:6   
x
[1] 2 3 4 5 6
y <- 2.5:6
y
[1] 2.5 3.5 4.5 5.5

In the first case, since the starting value is integer, the sequence operator : generate a sequence of integers from 2 to 6, whereas in the second case the starting value is float, the sequence operator : generate a sequence of floats ranging from 2.5 to 6.

z <- 2.5:6.9
z
[1] 2.5 3.5 4.5 5.5 6.5

It will generate a sequence of real numbers starting with 2.5 to 6.5.

# generate a sequence of integers in increasing order
x <- -3:5  
x
[1] -3 -2 -1  0  1  2  3  4  5
# generates a sequence of integers in decreasing order
x <- 5:-3
x
[1]  5  4  3  2  1  0 -1 -2 -3

(as precedence of -(unary minus) is higher than that of :).

Generating Vectors using seq() function

The operator : generate regular sequence of numbers in increasing or decreasing orders. The sequence of numbers with specific pattern can be generated using seq() function.

The general syntax of seq() function is

  • seq(from, to) or
  • seq(from, to, by=) or
  • seq(from, to, length=).

Examples of seq() function

Below R code generate a sequence of numbers from 1 to 8.

seq(1, 8)  
[1] 1 2 3 4 5 6 7 8

To generate a sequence from 1 to 8 with increment 2, use the below R code.

seq(1, 8, by = 2)  
[1] 1 3 5 7

Below R code generate a sequence of numbers from 8 to 1 with decrements 2

seq(8, 1, by = -2) 
[1] 8 6 4 2

Below R code generate a sequence of 3 numbers (because length argument is specified) from 1 to 8 with equal spacing.

seq(1, 8, length = 3) 
[1] 1.0 4.5 8.0

Below R code generate a sequence of numbers from 0 to 2 with length 5.

seq(0, 2, length = 5)
[1] 0.0 0.5 1.0 1.5 2.0

Below R code generate a sequence of numbers from 0 to 2 with length 10. The by value is calculated using the formula $\frac{\text{to}-\text{from}}{\text{length}-1}=0.2222222$.

seq(from = 0, to = 2, length = 10)
 [1] 0.0000000 0.2222222 0.4444444 0.6666667 0.8888889 1.1111111 1.3333333
 [8] 1.5555556 1.7777778 2.0000000

Repeating vectors using rep() function

A very useful function to create a vector by repeating a given number/vector with specified number of times is the rep() function.

The general structure of rep() function is

rep(v1,n1).

Here vector v1 is repeated n1 times.

The forms of rep() function are

  • rep(v1, times=),
  • rep(v1, each=),
  • rep(v1, length=).

Examples of rep() function

Below R code replicate 0 five times.

# replicates 0 five times
rep(0, 5)  
[1] 0 0 0 0 0

Below R code replicate 1, 2, 3 numbers 3 times.

# replicates 1 to 3 numbers 3 times
rep(1:3, times = 3)
[1] 1 2 3 1 2 3 1 2 3

Below R code replicate each element from the sequence 1,2 3 three times .

# each number 1 to 3 is replicated three times
rep(1:3,each=3) 
[1] 1 1 1 2 2 2 3 3 3

Below R code generate a vector 1:3 and store it in x. The next R code replicate the elements of x until you get the length 5. Here R recycle the elements of x till the length becomes 5.

# generate a vector 1,2,3
x <- 1:3
# vector x is replicated to get length five.
rep(x, length = 5)  
[1] 1 2 3 1 2

Below R code replicate each element of x twice until you get length 4. As the length is given as 4, R take each element twice and replicate the elements till the length becomes 4.

# replicate each element of x twice
# until you get length of 4
rep(x, each = 2, length = 4) 
[1] 1 1 2 2

The R code below replicate each element of x twice until you get length of 8. As there are only 3 elements 1,2 and 3 in x, R use recycling to get the length of 8.

rep(x, each = 2, length = 8) 
[1] 1 1 2 2 3 3 1 1

Below R code replicate each element of x twice and repeat the process six times.

rep(x, each = 2, times = 6)
 [1] 1 1 2 2 3 3 1 1 2 2 3 3 1 1 2 2 3 3 1 1 2 2 3 3 1 1 2 2 3 3 1 1 2 2 3 3

Below R code replicate each element of x one by one six times until the length becomes 10.

rep(x, each = 6, length = 10)
 [1] 1 1 1 1 1 1 2 2 2 2

Below R code replicate element of x such that first element replicated 2 times, second element replicated 1 time and third element replicated 3 times.

# 1 is replicated 2 times, and so on
rep(x, c(2, 1, 3))  
[1] 1 1 2 3 3 3

Accessing Elements of Vectors

Any particular element or elements of vectors in R can be accessed using a square brackets [].

Accessing elements by Positive Indexing

The element of vector can be accessed by using positive index of the position of that element, or using sequence of positive index (for adjacent elements) of elements or using c() function with position of elements of vector.

Recall the age.1 vector defined earlier.

age.1 <- c(20L, 31L, 45L, 37L, 24L)
age.1
[1] 20 31 45 37 24

Below R code return the $3^{rd}$ element from age.1.

age.1[3]
[1] 45

Consecutive elements of vector can be accessed using : operator. Below R code returns first and second element from age.1.

age.1[1:2]
[1] 20 31

Non-consecutive elements of vector can be accessed using c() function. Below R code returns first and third element from age.1.

age.1[c(1, 3)]
[1] 20 45

Accessing elements by Negative Indexing

Below R code is used to access all the elements except the third element of age.1 vector.

age.1[-3]
[1] 20 31 37 24

Below R code returns all the elements except the elements from the 1 through 3 index.

age.1[-(1:3)]
[1] 37 24

Below R code returns all the elements except the first and fourth elements from age.1 vector.

age.1[-c(1, 4)]
[1] 31 45 24

Accessing elements by Logical Vector

In accessing elements of vector using logical vector, R will return only those elements from vector where the logical values of logical vector are TRUE.

For example, in the below R code, R returns the value of first and third element of age vector because the first and third value of the logical vector is TRUE.

age.1[c(TRUE, FALSE, TRUE, FALSE)]
[1] 20 45 24

We can also use logical expression to access the elements of a vector. First, R evaluate the logical expression and create a logical vector. Then R returns Based on the value of the For example,

age.1[age.1 < 25]
[1] 20 24

In the above R code, the logical expression age.1<25 returns a logical vector (TRUE, FALSE, FALSE, FALSE, TRUE). Based on the result of logical vector, R returns those elements of age.1 for which the logical value is TRUE.

Accessing elements by Character Vector

The elements of named vector can be assessed by the name of the elements. Earlier we have seen how to give names to the elements of a vector.

Weight <- c(46, 58, 75, 49, 67)
Weight
[1] 46 58 75 49 67

Using names() function, we can assign names to the elements of vector.

names(Weight) <- c("Jatin", "Shruti", "David", "Parag", "Daniel")
Weight
 Jatin Shruti  David  Parag Daniel 
    46     58     75     49     67 

Below R code, return the weights of David and Daniel from the weight vector.

Weight[c("David", "Daniel")]
 David Daniel 
    75     67 

Coercion in vectors

When you mix different data values (like numeric, character, logical or complex) in a vector, R will coerce them so that they are all of the same type.

R follows two basic rules of coercion:

  • If a character is present in a vector, R will coerce everything else to characters.
  • If a vector contains logical values and numbers, R will convert the logical values to numbers (Like TRUE to 1 and FALSE to 0).

When different objects are mixed in a vector, coercion occurs so that every element in the vector is of the same type.

Vector with character and numeric values

Suppose the vector contains some character and some numeric values. In such a case R coerce all the numeric values to character and consider that vector as a character vector.

## xx is a character vector
xx <- c(1.8, "Male", 20)
xx
[1] "1.8"  "Male" "20"  

Since in the above R code two elements are numeric and one is character string, R will coerce all the elements to character and consider that vector as a character vector.

class(xx)
[1] "character"
as.numeric(xx)
Warning: NAs introduced by coercion
[1]  1.8   NA 20.0

The above R code convert vector xx to numeric. Since one of the element is character, R introduce NA by coercion in place of character and consider the vector xx as numeric vector.

Vector with numeric and logical values

Suppose the vector contains some numeric and some logical values. In such a case R coerce all the logical values to their numerical equivalent and consider that vector as a numeric vector. The numerical equivalent for TRUE or T is 1 and for FALSE or F is 0.

## yy is a numeric vector
yy <- c(TRUE, 1.8, -1.2, FALSE, T)
yy
[1]  1.0  1.8 -1.2  0.0  1.0
class(yy)
[1] "numeric"

If we convert yy numeric vector using as.logical() function, R will convert 0 to FALSE and all other values to TRUE.

as.logical(yy)
[1]  TRUE  TRUE  TRUE FALSE  TRUE

Vector with character and logical values

Suppose the vector contains some character and some logical values. In such a case R coerce all the logical values to character and consider that vector as a character vector.

## zz is a character vector
zz <- c("Male", TRUE)
zz
[1] "Male" "TRUE"

If we convert zz character vector using as.logical() function, R will convert character values to NA and the logical values to their logical equivalent.

as.logical(zz)
[1]   NA TRUE

Endnote

In this tutorial you learned about what is vector data structure in R, how to create vectors in R, how to initialize vectors in R and how to create vectors using various functions in R with some illustrations.

To learn more about other data structures in R, please refer to the following tutorials:

Hopefully you enjoyed learning this tutorial on vector type data structure in R. Hope the content is more than sufficient to understand vector in R.

VRCBuzz co-founder and passionate about making every day the greatest day of life. Raju is nerd at heart with a background in Statistics. Raju looks after overseeing day to day operations as well as focusing on strategic planning and growth of VRCBuzz products and services. Raju has more than 25 years of experience in Teaching fields. He gain energy by helping people to reach their goal and motivate to align to their passion. Raju holds a Ph.D. degree in Statistics. Raju loves to spend his leisure time on reading and implementing AI and machine learning concepts using statistical models.

Leave a Comment