A vector in R is a set of numbers. A vector can be considered as a single column or a single row of a spreadsheet. The following examples are numbers that are not technically “vectors”. It is because these vectors are not in a column/row structure, however, they are ordered. These vectors can be referred to by index.
Table of Contents
In R programming, vectors are the most basic data structure and a core building block of data analysis. Whether you’re new to R or brushing up on concepts, understanding vectors is essential. They form the building blocks for more complex structures like matrices, lists, and data frames.
Key Characteristics of Vectors
- Support Vectorized Operations: Arithmetic and logical operations can be applied element-wise without loops.
- Homogeneous: All elements must be of the same data type (such as numeric, character, logical, etc.).
- Indexed: Elements can be accessed using indices (starting at 1).
- Dynamic: Vectors can grow or shrink in size.
Types of Vectors in R Language
R supports several types of vectors based on the data they store:
(a) Numeric Vectors: Store real numbers (decimals or integers). For example: > c(1.5, 2.3, 4.0)
(b) Integer Vectors: Store whole numbers (explicitly defined with L
). For example, > c(1L, 2L, 3L)
(c) Logical Vectors: Store TRUE
, FALSE
, or NA
(missing value). For example: > c(TRUE, FALSE, NA)
(d) Character Vectors” Store text (strings). For example: > c("apple", "banana", "cherry")
(e) Complex Vectors: Store complex numbers. For example: > c(1+2i, 3+4i)
Creating Vectors in R
One can create vectors in R Language using:
c()
functionseq()
:
operator
# Creating a vector with the c() function c(1, 4, 6, 7, 9) c(1:5, 10)
A vector in R language can be created using seq()
in R, it generates a series of numbers.
# Create a vector using seq() in R seq(1, 10, by = 2) seq(0, 50, length = 11) seq(1, 50, length = 11)
The vector can be created in R using the colon (:) operator. Following are the examples
# Create vector in R using : operator 1:10 ## Output [1] 1 2 3 4 5 6 7 8 9 10 5:1 ## Output [1] 5 4 3 2 1
Creating Non-Integer Sequences in R
The non-integer sequences can also be created in the R Language.
# non-integer sequences seq(0, 100*pi, by = pi)
Assigning Vector to Variable
One can assign a vector to a variable using the assignment operator (<-) or equal symbol (=). The examples are:
a <- 1:5 b <- seq(15, 3, length=5) c <- a * b
Performing Computation on Vectors
There are a lot of built-in functions that can be used to perform different computations on vectors. For example,
a <- 1:5 # compute the total of elements of a vector sum(a) ## Output 15 # product of elements of a vector prod(a) ## Output 120 # average of the vector mean(a) ## Output 3 # standard deviation and variance of a vector sd(a) ## Output 1.581139 var(a) ## Output 2.5
Indexing and Slicing Vectors
One can extract the elements of a vector by using square brackets and the index of the component of the vector.
V <- seq(0, 100, by = 10) V[] # gives all the elements of the vector ## Output [1] 0 10 20 30 40 50 60 70 80 90 100 V[5] # 5th elements from vector z ## Output [1] 40 V[c(2, 4, 6, 8)] #2nd, 4th, th, and 8th element ## Output [1] 10 30 50 70 V[-c(2, 4, 6, 8)] # elements except 2nd, 4th, 6th, and 8th element ## Output [1] 0 20 40 60 80 90 100
Updating Vector Elements
The specific / required elements of a vector can be updated
V[c(2, 4)] <- c(500, 600) # the second and 4th element is updated to 500 and 600
Special Vector Values
The following are special vector values used in R Language.
Special Value | Meaning | Example |
---|---|---|
NA | Missing value | c(1, NA, 3) |
NaN | Not a Number | 0/0 → NaN |
Inf | Infinity | 1/0 → Inf |
NULL | Empty object | vector() → NULL |
Important Points About Vectors
The important points about vectors in R language are:
- Data Types: Vectors can hold logical, integer, double, character, complex, or raw data.
- Creation: Use the c() function to combine elements into a vector.
- Accessing Elements: Use indexing (square brackets) to access individual elements.
- Vector Operations: Perform arithmetic, logical, and comparison operations on vectors.
- Vectorization: R excels at vectorized operations, making calculations efficient.