Special Values and Types

Loading concept...

R’s Special Values: The Magic Boxes 📦

Imagine you have a toy box. Sometimes the box is empty, sometimes a toy is broken, and sometimes things just don’t make sense. R has special values for all these situations!


The Story of Four Special Friends

Think of R as a very organized librarian. This librarian has special labels for unusual situations:

  • NULL = “The shelf is completely empty”
  • NA = “A book is missing from its spot”
  • NaN = “This doesn’t make any sense!”
  • Inf = “This goes on forever!”

Let’s meet each one!


1. NULL: The Empty Box 📦

What is it? NULL means “nothing exists here.” Not even a placeholder. It’s like asking for a toy that was never put in the box.

Simple Example:

empty_box <- NULL
print(empty_box)
# NULL

# Check if something is NULL
is.null(empty_box)
# TRUE

Real Life:

  • You ask: “What’s in my invisible pocket?”
  • R says: NULL (there IS no pocket!)

When You See NULL:

  • A function returns nothing
  • You haven’t assigned a value yet
  • A list element doesn’t exist

2. NA: The Missing Puzzle Piece 🧩

What is it? NA means “Not Available.” The spot exists, but we don’t know what goes there. Like a puzzle with one piece missing.

Simple Example:

ages <- c(10, NA, 12, 8)
print(ages)
# 10 NA 12 8

# Check for NA
is.na(ages)
# FALSE TRUE FALSE FALSE

Real Life:

  • Survey question: “How old is your pet?”
  • Someone didn’t answer: that’s NA

The Tricky Part:

# NA spreads like a mystery!
5 + NA
# NA (We don't know the answer)

mean(c(1, 2, NA))
# NA (Can't calculate with missing data)

# Fix it with na.rm = TRUE
mean(c(1, 2, NA), na.rm = TRUE)
# 1.5 (Now it ignores NA!)

3. NaN: The Confused Calculator 🤯

What is it? NaN means “Not a Number.” It happens when math goes crazy and produces something that isn’t a real number.

Simple Example:

weird <- 0 / 0
print(weird)
# NaN

# Check for NaN
is.nan(weird)
# TRUE

Real Life:

  • Asking: “What’s nothing divided by nothing?”
  • Math says: “That doesn’t make sense!” = NaN

Important Note:

# NaN is also NA, but NA is not NaN
x <- NaN
is.na(x)   # TRUE
is.nan(x)  # TRUE

y <- NA
is.na(y)   # TRUE
is.nan(y)  # FALSE

4. Inf: The Never-Ending Number 🚀

What is it? Inf means “Infinity.” A number so big it goes on forever! Also comes in negative: -Inf

Simple Example:

huge <- 1 / 0
print(huge)
# Inf

tiny <- -1 / 0
print(tiny)
# -Inf

# Check for infinity
is.infinite(huge)
# TRUE

Real Life:

  • Counting all the stars in the universe
  • The number just keeps going: Inf

Fun Facts:

Inf + 100     # Inf (Still infinite!)
Inf - Inf     # NaN (Confusion!)
Inf > 999999  # TRUE (Always bigger)

The Family Portrait

graph TD A[Special Values] --> B[NULL] A --> C[NA] A --> D[NaN] A --> E[Inf] B --> B1[Nothing exists] C --> C1[Value unknown] D --> D1[Math error] E --> E1[Endless number]

Type Checking: The Detective Tools 🔍

R gives you special magnifying glasses to figure out what type of value you’re looking at!


is.null() - “Is the box empty?”

x <- NULL
y <- 5

is.null(x)  # TRUE
is.null(y)  # FALSE

is.na() - “Is something missing?”

data <- c(1, NA, 3)

is.na(data)
# FALSE TRUE FALSE

# Count missing values
sum(is.na(data))
# 1

is.nan() - “Did math go crazy?”

values <- c(1, NaN, Inf)

is.nan(values)
# FALSE TRUE FALSE

is.infinite() - “Does it go forever?”

nums <- c(5, Inf, -Inf, 0)

is.infinite(nums)
# FALSE TRUE TRUE FALSE

is.finite() - “Is it a normal number?”

test <- c(1, NA, NaN, Inf)

is.finite(test)
# TRUE FALSE FALSE FALSE

Type Checking for Data Types

Function Checks For Example
is.numeric() Numbers is.numeric(5) → TRUE
is.character() Text is.character("hi") → TRUE
is.logical() TRUE/FALSE is.logical(TRUE) → TRUE
is.vector() Vector is.vector(c(1,2)) → TRUE
is.list() List is.list(list(a=1)) → TRUE
is.data.frame() Data Frame is.data.frame(df) → TRUE

Quick Example:

x <- 42
is.numeric(x)    # TRUE
is.character(x)  # FALSE

y <- "hello"
is.numeric(y)    # FALSE
is.character(y)  # TRUE

Type Conversion: The Magic Transformer ✨

Sometimes you need to change one type into another. It’s like turning a caterpillar into a butterfly!


as.numeric() - Turn into Number

text_num <- "42"
real_num <- as.numeric(text_num)
print(real_num)
# 42

# Can do math now!
real_num + 8
# 50

Be Careful!

as.numeric("hello")
# NA (Can't turn words into numbers!)

as.character() - Turn into Text

number <- 123
text <- as.character(number)
print(text)
# "123"

# Now you can paste it!
paste("Room", text)
# "Room 123"

as.logical() - Turn into TRUE/FALSE

as.logical(1)    # TRUE
as.logical(0)    # FALSE
as.logical(-5)   # TRUE (any non-zero)

as.logical("TRUE")   # TRUE
as.logical("FALSE")  # FALSE

as.integer() - Turn into Whole Number

decimal <- 3.7
whole <- as.integer(decimal)
print(whole)
# 3 (Cuts off the decimal!)

Conversion Cheat Table

From To Function Example
Text Number as.numeric() as.numeric("5") → 5
Number Text as.character() as.character(5) → “5”
Number Logical as.logical() as.logical(1) → TRUE
Logical Number as.numeric() as.numeric(TRUE) → 1
Decimal Integer as.integer() as.integer(3.9) → 3

Type Introspection: Looking Inside 👀

Ever wondered what something really is? R has tools to peek inside any value!


typeof() - The True Identity

Shows the internal storage type.

typeof(5)        # "double"
typeof(5L)       # "integer"
typeof("hi")     # "character"
typeof(TRUE)     # "logical"
typeof(NULL)     # "NULL"

class() - The Public Name

Shows how R treats the object.

class(5)           # "numeric"
class("hi")        # "character"
class(TRUE)        # "logical"
class(data.frame())# "data.frame"

mode() - The Category

Shows the basic mode of storage.

mode(5)      # "numeric"
mode("hi")   # "character"
mode(TRUE)   # "logical"
mode(NULL)   # "NULL"

str() - The Full Story

Shows structure of any object.

my_list <- list(name = "R",
                version = 4.3)
str(my_list)
# List of 2
#  $ name   : chr "R"
#  $ version: num 4.3

summary() - Quick Overview

Great for data exploration.

nums <- c(1, 5, 3, NA, 10)
summary(nums)
#    Min. 1st Qu.  Median    Mean
#    1.00    2.00    4.00    4.75
#    3rd Qu.    Max.    NA's
#    7.50   10.00       1

The Introspection Family

graph TD A[Want to Know Type?] --> B{What Info?} B --> C[Internal Type] B --> D[Object Class] B --> E[Full Structure] C --> C1[typeof] D --> D1[class] E --> E1[str]

Putting It All Together 🎯

The Complete Detective Kit

mystery <- c(1, NA, Inf, NaN)

# What is it?
typeof(mystery)    # "double"
class(mystery)     # "numeric"

# Find the special values
is.na(mystery)     # F T F T
is.nan(mystery)    # F F F T
is.infinite(mystery) # F F T F
is.finite(mystery) # T F F F

Quick Reference

I Want To… Use This
Check if empty is.null(x)
Check if missing is.na(x)
Check if math error is.nan(x)
Check if infinite is.infinite(x)
Check if normal number is.finite(x)
See internal type typeof(x)
See object class class(x)
See full structure str(x)
Convert to number as.numeric(x)
Convert to text as.character(x)
Convert to logical as.logical(x)

Remember! 🌟

  1. NULL = Nothing exists (the box doesn’t exist)
  2. NA = Value is missing (the box is empty)
  3. NaN = Math went wrong (the box has garbage)
  4. Inf = Goes on forever (the box is infinite!)

Use is.*() functions to check, as.*() functions to convert, and typeof()/class()/str() to investigate!

You now have all the tools to handle R’s special values like a pro! 🚀

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.