๐ R Arrays: Your Magic Backpack of Data!
The Story of Arrays
Imagine you have a magic backpack that can hold many things in neat little compartments. Thatโs exactly what an array is in R!
Think of it like this: You have a box of crayons. Each crayon has its own spot, numbered 1, 2, 3โฆ You can grab any crayon by saying โGive me crayon number 3!โ Thatโs how arrays work!
๐จ What is an Array?
An array is like a multi-shelf storage box.
| Real Life | In R |
|---|---|
| A bookshelf with rows and columns | A 2D array |
| A stack of bookshelves | A 3D array |
| Numbered lockers in a row | A 1D array (vector) |
Key Idea: Arrays store the same type of things (all numbers, all words) in organized compartments you can access by number.
๐๏ธ Creating Arrays
Method 1: The array() Function
This is your main tool for building arrays!
# Simple 1D array (like a row of lockers)
my_array <- array(1:6, dim = c(6))
# Result: 1 2 3 4 5 6
# 2D array (like a grid of boxes)
matrix_array <- array(1:6, dim = c(2, 3))
# Result:
# [,1] [,2] [,3]
# [1,] 1 3 5
# [2,] 2 4 6
Whatโs happening?
1:6= The stuff to put inside (numbers 1 through 6)dim = c(2, 3)= The shape: 2 rows, 3 columns
Method 2: 3D Arrays (The Magic Cube!)
# A 3D array is like stacked grids
cube <- array(1:12, dim = c(2, 3, 2))
# Creates 2 layers, each with 2 rows & 3 cols
Think of it as two sheets of paper, each with a 2ร3 grid drawn on it!
graph TD A[3D Array] --> B[Layer 1] A --> C[Layer 2] B --> D[Row 1: 1, 3, 5] B --> E[Row 2: 2, 4, 6] C --> F[Row 1: 7, 9, 11] C --> G[Row 2: 8, 10, 12]
Method 3: Using dim() on a Vector
You can transform a simple list into an array!
# Start with a vector
my_vector <- c(10, 20, 30, 40, 50, 60)
# Add dimensions to make it an array
dim(my_vector) <- c(2, 3)
# Now it's a 2x3 array!
๐ฏ Pro Tip: R fills arrays column by column (top to bottom, then left to right), not row by row!
๐ง Array Operations
Now letโs learn how to use your magic backpack!
1. Accessing Elements (Finding Your Stuff)
# Create a 2x3 array
arr <- array(c(5, 10, 15, 20, 25, 30),
dim = c(2, 3))
# Get one item: row 1, column 2
arr[1, 2] # Returns: 15
# Get entire row 1
arr[1, ] # Returns: 5 15 25
# Get entire column 2
arr[, 2] # Returns: 15 20
Think of it like: โI want the item at Row 1, Shelf 2!โ
2. Modifying Elements (Changing Things)
# Change one value
arr[1, 1] <- 100
# Now position [1,1] holds 100!
# Change an entire row
arr[2, ] <- c(7, 8, 9)
# Row 2 is now: 7, 8, 9
3. Math with Arrays (The Fun Part!)
Arrays love math! You can do operations on everything at once.
arr1 <- array(1:4, dim = c(2, 2))
arr2 <- array(5:8, dim = c(2, 2))
# Add them together
arr1 + arr2
# Each position adds up!
# Multiply everything by 2
arr1 * 2
# Every number doubles!
graph TD A["arr1<br>[1,3]<br>[2,4]"] --> |"+ arr2"| B["Result<br>[6,10]<br>[8,12]"] C["arr2<br>[5,7]<br>[6,8]"] --> B
4. Useful Array Functions
| Function | What It Does | Example |
|---|---|---|
dim() |
Shows dimensions | dim(arr) โ 2 3 |
length() |
Total elements | length(arr) โ 6 |
sum() |
Adds all values | sum(arr) โ 105 |
mean() |
Average value | mean(arr) โ 17.5 |
5. The apply() Magic
Want to do something to each row or column? Use apply()!
arr <- array(1:6, dim = c(2, 3))
# Sum each row (MARGIN = 1)
apply(arr, 1, sum)
# Returns: 9 12
# Sum each column (MARGIN = 2)
apply(arr, 2, sum)
# Returns: 3 7 11
Remember:
MARGIN = 1โ Do it to ROWSMARGIN = 2โ Do it to COLUMNS
๐ Quick Summary
| Task | Code | Result |
|---|---|---|
| Create array | array(1:6, c(2,3)) |
2ร3 grid |
| Get item | arr[1,2] |
Value at row 1, col 2 |
| Get row | arr[1,] |
Entire first row |
| Get column | arr[,2] |
Entire second column |
| Change item | arr[1,1] <- 99 |
Updates that spot |
| Add arrays | arr1 + arr2 |
Element-wise sum |
| Row sums | apply(arr, 1, sum) |
Sum of each row |
๐ก Why Arrays Matter
Arrays are the building blocks for:
- ๐ Data tables
- ๐ผ๏ธ Images (pixels in a grid!)
- ๐ฎ Game boards
- ๐ Scientific data
Youโre now ready to organize data like a pro! ๐
๐ฏ Key Takeaways
- Arrays = organized storage with numbered compartments
- Create with
array()- give it data and dimensions - Access with brackets -
[row, column, layer] - R fills column-first - remember this quirk!
apply()is your friend for row/column operations
Now go fill those magic backpacks with data! ๐โจ