4. Modes (types) - JulTob/R GitHub Wiki
Ahoy! Ye’ve hauled in a treasure trove: modes, classes, structures... the very backbone of R’s strange and powerful type system. And ye askin' the right question: what the devil is a “mode”?
🧠 Clarifyin’ the Mystery of “Mode” vs “Class”
In R, we have:
- mode(): tells ye the storage mode, the low-level type R uses internally.
- class(): tells ye the higher-level behavior, like whether an object acts like a factor, data frame, model, etc.
So in most cases:
- A vector of numbers will have mode = "numeric" and class = "numeric".
- A factor has mode = "numeric" but class = "factor" — sneaky!
- A data.frame has mode = "list" but class = "data.frame"
Most sailors just use class() in modern R, as it reflects what R thinks the object is.
🧭 Compass of Data Modes and Structures
“Know not just what yer objects are, but how they behave and what cargo they carry!”
⸻
🔸 Basic Modes (Primitive Data Types)
These be the fundamental types in R, used to build all else.
1. Logical
T
F
Short for TRUE and FALSE, these are used in comparisons, conditions, and logical operations.
2. Numeric
> a + b
> a - b
> a * b
> a / b
> a^b
> sqrt(a)
> exp(1)
```R
Arithmetic with numeric values works as expected.
Comparisons return logical results:
```R
> a == b
> a != b
> a < b
> a >= b
3. Complex
> z <- 2 + 3i
> class(z)
[1] "complex"
Used rarely, but available if ye sail in mathematical waters.
4. Character
'a'
"yo-ho-ho"
Strings be marked with single or double quotes.
⸻
🧰 Composite Structures (Object Containers)
Here be the containers that hold yer values, either singly or in bulk.
⸻
🪙 Vector
A vector be a ledger of values, all of the same type.
> vect <- c(1, 2, 3, 5, 7)
> mode(vect)
[1] "numeric"
> length(vect)
[1] 5
Vectors can be numeric, character, logical, or even complex:
> starks <- c("Rob", "John", "Ned", "Sansa")
> mode(starks)
[1] "character"
Access elements by position:
> x <- c(-5:5)
> x[1:3]
[1] -5 -4 -3
Modify elements:
> x[2] <- 0
Vectors can be named, makin’ them like small dictionaries:
> z <- c(5, 3, 4)
> names(z) <- c("hypot", "cat1", "cat2")
> z
hypot cat1 cat2
5 3 4
Vector arithmetic is element-wise:
> x1 <- c(0:3)
> x2 <- c(1:4)
> x1 + x2
[1] 1 3 5 7
⸻
🧱 Matrix
A matrix be a 2D rectangular block of one type only:
> A <- matrix(c(1, 2, 3, 4), ncol = 2)
You can bind columns or rows:
> B <- cbind(c(1:3), c(4:6))
> t(B) # Transpose
Give it row/column names for clarity:
> C <- matrix(c(1:8), nrow=2,
dimnames=list(c("Fila1","Fila2"),
c("Col1","Col2","Col3","Col4")))
⸻
🎭 Factor
Used to represent categories like "Yes"/"No", "Red"/"Blue":
> coin <- factor(c("heads", "tails", "heads"))
> class(coin)
[1] "factor"
These look like strings but behave as indexed levels.
⸻
📊 Data Frame
A data frame be like a spreadsheet: columns of mixed types.
> df <- data.frame(name = c("Arya", "Bran"), age = c(16, 14))
Each column is a vector, but they can differ in type. R prints these as tables.
⸻
🔀 List
The most flexible object: holds anything.
> my_list <- list(name = "Brienne", age = 32, swords = c("Oathkeeper", "Widow’s Wail"))
Lists can contain vectors, data frames, even other lists. It be the cargo hold of the ship.
⸻
⚖️ Summary Table of Modes vs Structures
| Structure | Homogeneous? | Mixed Types? | 2D? | Typical Class |
|---|---|---|---|---|
| Vector | ✅ | ❌ | ❌ | “numeric”, etc. |
| Matrix | ✅ | ❌ | ✅ | “matrix” |
| List | ❌ | ✅ | ❌ | “list” |
| Data Frame | Columns can vary | ✅ | ✅ | “data.frame” |
| Factor | ✅ (underlying numeric) | ❌ | ❌ | “factor” |
With this chart in yer hands, ye can now peer into the soul of any object in R, and choose the right vessel to carry yer data forward.