What are Vectors in R?

// Quick Answer
  • A vector is the most basic data structure in R.
  • It stores elements of the same type (numeric, character, logical).
  • Created using the c() function.
  • R performs operations on vectors element-by-element.
  • It is the foundation of data analysis in R.

What are vectors in R?

In R, a vector is the simplest and most important data structure. It is a collection of elements that are all of the same type, such as numbers, text, or logical values.

💡 Simple idea

A vector is like a row of values stored in a single variable.

How to create a vector

You create vectors in R using the c() function (combine function).

R
numbers <- c(1, 2, 3, 4, 5)

This creates a numeric vector with five elements.

Types of vectors

  • 🔢 Numeric vectors — numbers (e.g. 1, 2, 3)
  • 🔤 Character vectors — text (e.g. "apple", "banana")
  • ✔️ Logical vectors — TRUE / FALSE values

Examples

R
# Numeric vector
nums <- c(10, 20, 30)

# Character vector
names <- c("Asha", "Rahul", "Meena")

# Logical vector
flags <- c(TRUE, FALSE, TRUE)

How vectors work

One powerful feature of R is that operations are applied to every element of a vector automatically.

R
nums <- c(1, 2, 3)

nums * 2

Output: 2 4 6

Indexing vectors

You can access elements using square brackets [].

R
nums <- c(10, 20, 30)

nums[1] # 10

Why vectors matter

  • 📊 Core building block of R programming
  • ⚙️ Used in all data analysis tasks
  • 🚀 Enables fast mathematical operations
  • 📦 Basis for matrices, lists, and data frames
📌 Real-world fact

Almost everything in R — from data frames to statistical models — is built on vectors.

Summary

Vectors in R are basic structures that store elements of the same type. They allow fast operations and form the foundation of data analysis in R.

In short: A vector is a simple, powerful container for data in R.