CHAPTER 04
Beginner
Variables and Data Types in R
Updated: May 18, 2026
5 min read
# CHAPTER 4
Variables and Data Types in R
1. Chapter Introduction
R has five fundamental data types that form the foundation of all data structures. Understanding type behaviors — especially how R coerces types automatically — prevents data analysis bugs and type mismatch errors.2. R Data Types Overview
r
3. Factors — Categorical Data
r
4. Type Checking and Conversion
r
5. Special Values
r
6. Common Mistakes
-
NAvsNULL:NAis a missing value placeholder (has length 1, can be in vectors).NULLis the empty object (length 0, removes elements from lists). They behave very differently.
-
Using
==to check NA:x == NAalways returnsNA. Always useis.na(x)to test for missing values.
7. MCQs
Question 1
42L in R creates?
Question 2
as.integer(3.9) returns?
Question 3
is.na(NA) returns?
Question 4
Factors are useful for?
Question 5
R coercion: which type wins TRUE + 5L?
Question 6
nchar("RStudio") returns?
Question 7
Ordered factor enables?
Question 8
mean(c(1,2,NA,4), na.rm=TRUE) returns?
Question 9
NULL represents?
Question 10
class(TRUE) returns?
8. Interview Questions
-
Q: What is the difference between
NA,NULL, andNaNin R?
- Q: How do factors differ from character vectors?
9. Summary
R has 5 atomic types: numeric (double), integer (L suffix), character, logical (TRUE/FALSE), complex. Factors encode categorical data as integers with labels — efficient and orderable. Special values:NA (missing), NULL (empty), NaN (undefined math), Inf (infinity). Coercion hierarchy: logical < integer < numeric < complex < character. Always use is.na() not == NA.