R Data Types
🧩 R Data Types – Complete Tutorial
In R, data types define what kind of data a variable can store and how R interprets it.
Understanding data types is essential for data analysis, statistics, and interviews.
1️⃣ What are Data Types in R?
R has several basic (atomic) and complex data types.
👉 The most commonly used atomic data types are:
-
Numeric
-
Integer
-
Character
-
Logical
-
Complex
2️⃣ Numeric Data Type ⭐
-
Stores decimal or real numbers
-
Default number type in R
3️⃣ Integer Data Type ⭐
-
Stores whole numbers
-
Use
Lsuffix
4️⃣ Character Data Type ⭐
-
Stores text / strings
5️⃣ Logical Data Type ⭐
-
Stores TRUE or FALSE
-
Used in conditions
6️⃣ Complex Data Type ⭐
-
Stores complex numbers
7️⃣ Special Data Types in R ⚠️
NA – Missing Value
NULL – Empty / No Value
NaN – Not a Number
Inf – Infinity
8️⃣ Checking Data Types ⭐
9️⃣ Type Conversion (Coercion) ⭐
R automatically converts types when needed.
📌 Coercion hierarchy:
🔟 Vectors with Different Data Types ❌
📌 R converts all elements to character.
1️⃣1️⃣ Data Type vs Data Structure ⭐
| Data Type | Example |
|---|---|
| Numeric | 10, 3.5 |
| Character | “R” |
| Logical | TRUE |
| Data Structure | Example |
|---|---|
| Vector | c(1,2,3) |
| List | list(1,”R”,TRUE) |
| Matrix | matrix(1:4) |
| Data Frame | data.frame() |
1️⃣2️⃣ Common Mistakes ❌
❌ Forgetting L for integer
❌ Mixing data types in vectors
❌ Confusing NA and NULL
❌ Ignoring implicit coercion
📌 Interview Questions & MCQs
Q1. Default numeric type in R?
A) Integer
B) Double
C) Float
D) Logical
✅ Answer: B
Q2. Which data type stores text?
A) Numeric
B) Logical
C) Character
D) Integer
✅ Answer: C
Q3. Which value represents missing data?
A) NULL
B) NaN
C) NA
D) Inf
✅ Answer: C
Q4. What happens when mixing data types in vector?
A) Error
B) Warning
C) Coercion
D) Ignored
✅ Answer: C
Q5. Which is NOT an atomic data type?
A) Numeric
B) Character
C) Data Frame
D) Logical
✅ Answer: C
🔥 Real-Life Use Cases
✔ Data cleaning
✔ Data validation
✔ Statistical analysis
✔ Machine learning
✔ Data transformation
✅ Summary
-
It has 5 main atomic data types
-
Default numeric type is double
-
Special values:
NA,NULL,NaN,Inf -
R uses automatic type coercion
-
Understanding data types is critical for R programming & interviews
