Introduction to Vectors in R

Vectors are the most fundamental data structure in R. Almost everything in R—numbers, characters, logical values, and even matrices—is built on top of vectors. Understanding vectors deeply is essential to mastering R programming, data analysis, and statistical computing.

What is a Vector in R?

A vector is a one-dimensional data structure that stores a sequence of elements of the same data type. All elements in a vector must belong to the same type; if different types are mixed, R automatically converts them to a common type (this process is called type coercion).

Examples of vectors:

  • A list of numbers
  • A set of names
  • A sequence of TRUE/FALSE values

Why Vectors are Important in R

Vectors are important because:

  • R is a vectorized language
  • Operations on vectors are faster than loops
  • Most R functions expect vector inputs
  • Data frames and matrices are built from vectors

R encourages vectorized operations, which means you can operate on an entire vector at once without using loops.

Creating Vectors in R

Creating a Vector Using c() Function

The most common way to create a vector is using the c() (combine) function.

numbers <- c(10, 20, 30, 40)
names <- c("Alice", "Bob", "Charlie")
logical_values <- c(TRUE, FALSE, TRUE)

Creating an Empty Vector

You can create an empty vector and fill it later.

empty_vec <- c()

Or create a vector of a specific type and length:

numeric_vec <- numeric(5)
character_vec <- character(3)
logical_vec <- logical(4)

Types of Vectors in R

R supports atomic vectors, which include:

Numeric Vector

Stores decimal numbers.

x <- c(1.5, 2.3, 4.7)

Integer Vector

Stores integers (use L).

y <- c(1L, 2L, 3L)

Character Vector

Stores text strings.

z <- c("R", "Data", "Science")

Logical Vector

Stores TRUE/FALSE values.

flag <- c(TRUE, FALSE, TRUE)

Complex Vector

Stores complex numbers.

cplx <- c(2+3i, 4+5i)

Checking the Type of a Vector

Use class() or typeof().

class(numbers)
typeof(numbers)

Vector Type Coercion

When different data types are combined in a vector, R automatically converts all elements to the most flexible type.

Coercion Hierarchy

logical → integer → numeric → character

Example:

mixed <- c(1, TRUE, "R")
print(mixed)

Output:

[1] "1" "TRUE" "R"

Vector Indexing

Indexing is used to access elements in a vector.

Indexing by Position

R uses 1-based indexing.

v <- c(10, 20, 30, 40)
v[1]    # First element
v[4]    # Fourth element

Indexing with Multiple Positions

v[c(1, 3)]

Negative Indexing

Negative indices remove elements.

v[-2]

Logical Indexing

Logical vectors can be used to filter data.

v[v > 20]

Naming Vector Elements

You can assign names to vector elements.

scores <- c(85, 90, 78)
names(scores) <- c("Math", "Science", "English")

Access by name:

scores["Math"]

Vector Recycling Rule

When performing operations on vectors of unequal length, R recycles the shorter vector.

Example:

v1 <- c(1, 2, 3, 4)
v2 <- c(10, 20)
v1 + v2

R repeats v2:

1+10, 2+20, 3+10, 4+20

Warning is issued if lengths are not multiples.

Vectorized Operations

R allows operations on entire vectors without loops.

v <- c(1, 2, 3, 4)
v * 2
v + 10

This is faster and cleaner than using loops.

Common Vector Functions

Length of Vector

length(v)

Sorting a Vector

sort(v)

Finding Minimum and Maximum

min(v)
max(v)

Sum and Mean

sum(v)
mean(v)

Creating Sequences of Vectors

Using :

1:10

Using seq()

seq(from = 1, to = 10, by = 2)

Using rep()

rep(1:3, times = 2)

Modifying Vectors

Updating Elements

v[2] <- 100

Replacing Based on Condition

v[v < 10] <- 0

Removing Elements from a Vector

v <- v[-3]

Checking Membership

Use %in% to check if elements exist.

5 %in% v

Practical Example

marks <- c(45, 67, 89, 90, 34)
passed <- marks[marks >= 40]
average <- mean(passed)

print(passed)
print(average)

Common Mistakes with Vectors

  • Mixing data types unintentionally
  • Forgetting R uses 1-based indexing
  • Ignoring recycling warnings
  • Using loops instead of vectorized operations

Summary

Vectors are the backbone of R programming. They store data in a one-dimensional format and support fast, vectorized operations. Understanding how to create, index, modify, and operate on vectors is crucial for working with data frames, matrices, and advanced statistical functions in R.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *