R Data Structures

🧱 R Data Structures

Data structures in R are used to store, organize, and manage data efficiently.

R is very powerful in data handling, which is why it’s popular in data analysis and statistics.


🔹 Main Data Structures in R

R mainly provides 6 important data structures:

  1. Vector

  2. List

  3. Matrix

  4. Array

  5. Data Frame

  6. Factor


1️⃣ Vector

  • One-dimensional

  • Stores same type of data only

v <- c(10, 20, 30)
names <- c("A", "B", "C")

✔ Types: numeric, character, logical

Access elements:

v[2]

2️⃣ List

  • Can store different data types

  • Most flexible structure

my_list <- list(
name = "R",
version = 4.3,
active = TRUE
)

Access:

my_list$name
my_list[[2]]

3️⃣ Matrix

  • Two-dimensional

  • All elements must be of same data type

m <- matrix(c(1,2,3,4), nrow = 2)
m

Access:

m[1,2]

4️⃣ Array

  • Multi-dimensional

  • Extension of matrix

arr <- array(1:8, dim = c(2,2,2))
arr

✔ Used in scientific & statistical computing


5️⃣ Data Frame ⭐ (Most Important)

  • Table-like structure

  • Columns can have different data types

  • Widely used in data analysis

df <- data.frame(
name = c("A", "B"),
age = c(20, 25),
pass = c(TRUE, FALSE)
)

Access:

df$name
df[1, ]
df[, 2]

6️⃣ Factor

  • Used for categorical data

  • Stores data as levels

gender <- factor(c("Male", "Female", "Male"))
gender

Check levels:

levels(gender)

🔹 Comparison Table

Structure Dimensions Data Type
Vector 1D Same
List 1D Different
Matrix 2D Same
Array Multi-D Same
Data Frame 2D Different
Factor 1D Categorical

🔹 Choosing the Right Data Structure

✔ Numeric sequence → Vector
✔ Mixed data → List
✔ Table data → Data Frame
✔ Categorical values → Factor
✔ Mathematical data → Matrix / Array


📌 Summary

  • Data structures help organize data

  • Data frame is the most commonly used

  • Each structure has a specific purpose

  • Choosing the right structure improves performance

You may also like...