Here, we will discuss vectors in R, how to create vectors, coerce vectors, and operations such as add, subtract, multiply or divide vectors.

This object is the primary building block of other objects in R. A vector contains homogeneous data types and it is 1-dimensional. Its composition can be one of the numeric (or double), integer, character, logical, complex or raw data types.

1 Create Vectors in R

Create a vector with numeric (or double) elements:

vec1 = c(1, 4, 9, 16, 25, 36, 49, 64, 81, 100)
vec1
 [1]   1   4   9  16  25  36  49  64  81 100

Create a vector with character elements:

vec2 = c("Red", "Blue", "Green")
vec2
[1] "Red"   "Blue"  "Green"

Create an empty vector:

vec3 = rep(NA, times = 5)
vec3
[1] NA NA NA NA NA

Assign values to (or edit) some index of a vector:

vec4 = rep(NA, times = 5)
vec4[1:3] = 10
vec4[5] = 100
vec4
[1]  10  10  10  NA 100
Summary of the Functions to Coerce or Check Vectors in R
Function Usage
length() Check or set a vector’s length
is.vector() Check if vector
typeof() Check data type
is.*type*() Check if vector is type
as.vector() Coerce into vector
as.*type*() Coerce vector into type
  • type is numeric, integer, character, logical, or complex.

  • See examples below.

2 Check Vectors in R

Check the length of a vector:

vec1 = c(1, 4, 9, 16, 25)
length(vec1)
[1] 5

Set or change the length of a vector:

vec1 = c(1, 4, 9, 16, 25)
vec1
[1]  1  4  9 16 25
length(vec1) = 10
vec1
 [1]  1  4  9 16 25 NA NA NA NA NA
length(vec1) = 3
vec1
[1] 1 4 9

Check if an object is a vector:

vec1 = c(1, 4, 9)
is.vector(vec1) # This will return TRUE or FALSE.
[1] TRUE

Check the data type or mode of a vector:

vec1 = c(10, 20, 30)
typeof(vec1)
[1] "double"
vec2 = c("A", "B", "C")
typeof(vec2)
[1] "character"

Check if a vector’s components are of a type/mode:

vec1 = c(10, 20, 30)
is.double(vec1)
[1] TRUE
is.integer(vec1)
[1] FALSE
vec2 = c("A", "B", "C")
is.character(vec2)
[1] TRUE

3 Coerce (into) Vectors in R

Coerce an object into a vector:

This example turns a matrix into a vector.

mat1 = matrix(c(1, 2, 4, 8, 16, 32),
              nrow = 3, byrow = TRUE)
mat1
     [,1] [,2]
[1,]    1    2
[2,]    4    8
[3,]   16   32
vec1 = as.vector(mat1) # This will turn into a vector by column.
vec1
[1]  1  4 16  2  8 32

Coerce a vector’s components into a different type/mode:

vec1 = c(10, 20, 30)
vec2 = as.integer(vec1)
typeof(vec2)
[1] "integer"
vec3 = as.character(vec1)
typeof(vec3)
[1] "character"

4 Add and Subtract Vectors in R

Add or subtract vectors of the same lengths:

vec1 = c(1, 2, 3, 4, 5)
vec1
[1] 1 2 3 4 5
vec2 = c(1:5)
vec2
[1] 1 2 3 4 5
# Add two vectors
vec1 + vec2
[1]  2  4  6  8 10
# Subtract a number from a vector
vec1 - 1
[1] 0 1 2 3 4

You can also add or subtract vectors of different lengths:

vec3 = rep(2, times = 8)
vec3
[1] 2 2 2 2 2 2 2 2
vec4 = c(1, 100, 1000)
vec4
[1]    1  100 1000
vec5 = c(0,1)
vec5
[1] 0 1
vec3 + vec4
Warning in vec3 + vec4: longer object length is not a multiple of shorter
object length
[1]    3  102 1002    3  102 1002    3  102
vec3 - vec5
[1] 2 1 2 1 2 1 2 1

5 Multiply and Divide Vectors in R

Multiply or divide vectors of the same lengths:

vec1 = c(6, 7, 8, 9, 10)
vec1
[1]  6  7  8  9 10
vec2 = c(6:10)
vec2
[1]  6  7  8  9 10
# Multiple two vectors
vec1 * vec2
[1]  36  49  64  81 100
# Divide a vector by a number
vec1 / 10
[1] 0.6 0.7 0.8 0.9 1.0

You can also multiply or divide vectors of different lengths:

vec3 = rep(2, times = 8)
vec3
[1] 2 2 2 2 2 2 2 2
vec4 = c(1, 2, 3)
vec4
[1] 1 2 3
vec5 = c(1, 2)
vec5
[1] 1 2
# Multiply two vectors
vec3 * vec4
Warning in vec3 * vec4: longer object length is not a multiple of shorter
object length
[1] 2 4 6 2 4 6 2 4
# Divide a vector by another
vec3 / vec5
[1] 2 1 2 1 2 1 2 1

6 Join Strings from Character Vectors in R

For character vectors, you can use the paste() or paste0() (with no spacing) to join components:

vec1 = c("Red", "Blue", "Green")
vec2 = rep("Color", times = 3)
vec1; vec2
[1] "Red"   "Blue"  "Green"
[1] "Color" "Color" "Color"
paste(vec1, vec2)
[1] "Red Color"   "Blue Color"  "Green Color"
# paste0 will not add spacing when joining the strings
paste0(vec1, vec2)
[1] "RedColor"   "BlueColor"  "GreenColor"
paste("The color", vec1, "is a primary color.")
[1] "The color Red is a primary color."   "The color Blue is a primary color." 
[3] "The color Green is a primary color."

Copyright © 2020 - 2024. All Rights Reserved by Stats Codes