Skip to main content
R Programming
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
12345678910111213141516171819202122232425262728293031323334
# ─── 5 ATOMIC DATA TYPES IN R ────────────────────────

# 1. Numeric (double) — default for all numbers
salary <- 75000.50
pi_val <- 3.14159
class(salary)   # "numeric"
typeof(salary)  # "double"
is.numeric(salary)  # TRUE

# 2. Integer — whole numbers (suffix L)
age    <- 28L
count  <- 1000L
class(age)    # "integer"
is.integer(age)   # TRUE
# Without L: age <- 28 creates numeric, not integer

# 3. Character — text/strings
name    <- "Alice Johnson"
city    <- &#039;New York'     # Single or double quotes OK
class(name)  # "character"
nchar(name)  # 13  (string length)

# 4. Logical — Boolean
is_active  <- TRUE
has_passed <- FALSE
class(is_active)  # "logical"
# Note: TRUE/FALSE (uppercase) only. true/false are INVALID

# 5. Complex — complex numbers
z <- 3 + 2i
class(z)   # "complex"
Re(z)      # 3  (real part)
Im(z)      # 2  (imaginary part)
Mod(z)     # 3.606  (modulus)

3. Factors — Categorical Data

r
12345678910111213141516171819202122232425262728
# Factors represent categorical variables
# Stored as integers with labels — memory efficient

# Nominal factor (no order)
dept <- factor(c("HR", "IT", "Finance", "IT", "HR", "HR"))
class(dept)   # "factor"
levels(dept)  # "Finance" "HR" "IT"  (alphabetical by default)
nlevels(dept) # 3
table(dept)   # Count of each level
# dept
# Finance      HR      IT
#       1       3       2

# Ordinal factor (ordered)
education <- factor(c("High School", "Bachelor", "Master", "PhD", "Bachelor"),
                     levels = c("High School", "Bachelor", "Master", "PhD"),
                     ordered = TRUE)
levels(education)  # "High School" "Bachelor" "Master" "PhD"
education[2] > education[1]  # TRUE (Bachelor > High School)

# Common factor operations
freq_table <- table(dept)           # Frequency table
prop.table(freq_table)              # Proportions
barplot(table(education))           # Quick bar chart

# Recoding factors
levels(dept)[levels(dept) == "IT"] <- "Technology"
print(dept)  # "Technology" levels replaced

4. Type Checking and Conversion

r
12345678910111213141516171819202122232425262728293031323334353637
# ─── TYPE CHECKING FUNCTIONS ─────────────────────────
x <- 42.5
is.numeric(x)    # TRUE
is.integer(x)    # FALSE
is.character(x)  # FALSE
is.logical(x)    # FALSE

# ─── TYPE CONVERSION (coercion) ──────────────────────
# Numeric → Integer
as.integer(3.9)    # 3 (truncates, does NOT round)
as.integer("42")   # 42 (string to integer)
as.integer(TRUE)   # 1
as.integer(FALSE)  # 0

# To Character
as.character(12345)  # "12345"
as.character(TRUE)   # "TRUE"
as.character(3.14)   # "3.14"

# To Numeric
as.numeric("3.14")   # 3.14
as.numeric(TRUE)     # 1
as.numeric(FALSE)    # 0
as.numeric("abc")    # NA with warning

# To Logical
as.logical(0)        # FALSE
as.logical(1)        # TRUE
as.logical(42)       # TRUE (any non-zero = TRUE)
as.logical("TRUE")   # TRUE
as.logical("FALSE")  # FALSE
as.logical("yes")    # NA

# R's automatic coercion hierarchy:
# logical < integer < numeric < complex < character
# TRUE + 5L = 6 (logical coerced to integer)
# 5L + 2.5 = 7.5 (integer coerced to numeric)

5. Special Values

r
12345678910111213141516171819202122232425262728293031
# ─── SPECIAL VALUES IN R ─────────────────────────────

# NA — Not Available (missing value)
x <- NA
is.na(x)    # TRUE
class(NA)   # "logical" (but adapts to context)
NA + 5      # NA (any operation with NA = NA)
mean(c(1, 2, NA, 4))         # NA
mean(c(1, 2, NA, 4), na.rm = TRUE)  # 2.33 ← correct

# NaN — Not a Number (undefined math)
0/0          # NaN
is.nan(0/0)  # TRUE
is.nan(NA)   # FALSE

# NULL — Empty object
y <- NULL
is.null(y)  # TRUE
length(NULL) # 0

# Inf / -Inf — Infinity
1/0          # Inf
-1/0         # -Inf
is.infinite(1/0)  # TRUE

# Checking all specials
vals <- c(1, NA, NaN, Inf, -Inf, 0)
cat("NA:    ", is.na(vals), "\n")
cat("NaN:   ", is.nan(vals), "\n")
cat("Inf:   ", is.infinite(vals), "\n")
cat("Finite:", is.finite(vals), "\n")

6. Common Mistakes

  • NA vs NULL: NA is a missing value placeholder (has length 1, can be in vectors). NULL is the empty object (length 0, removes elements from lists). They behave very differently.
  • Using == to check NA: x == NA always returns NA. Always use is.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, and NaN in 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.

10. Next Chapter Recommendation

In Chapter 5: Operators and Expressions, we master R's complete operator system — arithmetic, comparison, logical, and assignment operators.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·