01 / 22
R

R Data Types:
The Building Blocks

Master the four fundamental data types in R: numeric, integer, character, and logical: the foundation of every R program you will write.

R Programming

What Are Data Types?

Every value you store in R has a data type: a label that tells R what kind of information it is and what operations are allowed on it. Think of data types as categories that determine how R treats your data.

The Four Core Types

numeric
Decimal numbers like 3.14, -0.5, 100.0
integer
Whole numbers like 1L, 42L, -7L
character
Text strings like "hello", "R"
logical
Boolean: TRUE or FALSE

Why Types Matter

R behaves differently depending on the type. Adding two numbers gives a sum, but adding two characters crashes. Knowing types prevents bugs and helps you write correct, efficient code.

1
Operations depend on type: \( 5 + 3 = 8 \), but "5" + "3" produces an error.
2
Functions expect specific types: mean() requires numeric input, not character.
3
Memory efficiency: integers use less memory than doubles for whole numbers.

R's Type Hierarchy

logical integer numeric character Implicit coercion direction → (least general → most general) TRUE / FALSE 5L, -2L 3.14, 1e3 "hello" is.logical() is.integer() is.numeric() is.character()

The numeric Type

R's default number type is numeric (also called double). It stores decimal values using 64-bit floating-point representation. Even if you type 5, R stores it as 5.0 internally.

# Creating numeric values x <- 3.14 y <- 42 # still numeric (double)! z <- -1.5e3 # scientific notation: -1500 class(x) # [1] "numeric" class(y) # [1] "numeric" is.numeric(z) # [1] TRUE

The integer Type

Integers are whole numbers explicitly created with the L suffix or by using as.integer(). They use less memory than numeric values and are important when interfacing with other systems.

# Creating integer values a <- 5L # L suffix makes it integer b <- as.integer(3.7) # coerces: truncates to 3 class(a) # [1] "integer" is.integer(a) # [1] TRUE is.numeric(a) # [1] TRUE (integers ARE numeric!)

The character Type

Character values store text strings. They are always wrapped in single or double quotes. Even a single letter like "a" is a character string in R: there is no separate "char" type.

# Creating character values name <- "Alice" grade <- 'A+' # single quotes work too num_str <- "42" # looks like a number, but it's text! class(name) # [1] "character" nchar(name) # [1] 5 is.character(num_str) # [1] TRUE

The logical Type

Logical values are R's Boolean type: exactly TRUE or FALSE. They result from comparisons, and R treats them as 1 and 0 in numeric contexts.

# Creating logical values flag <- TRUE check <- 5 > 3 # comparison produces logical class(flag) # [1] "logical" as.numeric(TRUE) # [1] 1 as.numeric(FALSE) # [1] 0 sum(c(TRUE, FALSE, TRUE)) # [1] 2 (counts TRUEs)

numeric vs. integer

numeric (double) integer
Created by x <- 5 x <- 5L
Stores Decimal / floating point Whole numbers only
Memory 8 bytes per value 4 bytes per value
Precision ~15 significant digits Exact (up to \( \pm (2^{31}-1) \))
is.numeric()? TRUE TRUE
is.integer()? FALSE TRUE

Check and Convert Types

1
Check with class(): Returns the type name as a string. class(42) → "numeric".
2
Test with is.*() functions: Returns TRUE or FALSE. is.character("hi") → TRUE.
3
Convert with as.*() functions: Explicitly coerce types. as.numeric("3.14") → 3.14.
4
Handle failures: Invalid conversions return NA. as.numeric("abc") → NA with a warning.

Automatic Coercion in Vectors

R vectors can only hold one type. When you mix types with c(), R silently coerces everything to the most general type.

# Mixing logical + integer c(TRUE, 3L, 5L) # [1] 1 3 5 (logical → integer) # Mixing integer + numeric c(1L, 2.5) # [1] 1.0 2.5 (integer → numeric) # Mixing numeric + character c(42, "hello") # [1] "42" "hello" (numeric → character)

Example 1: Identifying Types

# What type is each variable? a <- 10 b <- 10L c <- "10" d <- TRUE class(a) # "numeric" class(b) # "integer" class(c) # "character" class(d) # "logical"

Example 2: Type Conversion

# Convert between types x <- "3.14" y <- as.numeric(x) # 3.14 (character → numeric) n <- 7.9 m <- as.integer(n) # 7 (truncated, NOT rounded) val <- 0 flag <- as.logical(val) # FALSE (0 → FALSE) flag2 <- as.logical(42) # TRUE (nonzero → TRUE) bad <- as.numeric("abc") # NA + warning!

Example 3: Practical Application

# Survey data: count "Yes" responses responses <- c("Yes", "No", "Yes", "Yes", "No") # Convert to logical for counting is_yes <- responses == "Yes" # [1] TRUE FALSE TRUE TRUE FALSE sum(is_yes) # 3 (count of TRUE) mean(is_yes) # 0.6 (proportion = 60%)

Common Mistakes

✕ Forgetting the L suffix
Writing 5 instead of 5L gives numeric, not integer. Use is.integer() to check.
✕ Math on character numbers
Trying \( \text{"5"} + 1 \) throws an error. Convert first: \( \text{as.numeric("5")} + 1 \).
✕ Assuming rounding
as.integer(3.9) returns 3, not 4. R truncates, it does not round.

Summary Table

Type Example Check Convert
numeric 3.14, 42 is.numeric() as.numeric()
integer 5L, -2L is.integer() as.integer()
character "hello", "42" is.character() as.character()
logical TRUE, FALSE is.logical() as.logical()

Question 1 of 5

What is the output of class(42) in R?

Question 2 of 5

What does as.integer(4.8) return?

Question 3 of 5

What is the result of c(1, 2, "three")?

Question 4 of 5

What does sum(c(TRUE, FALSE, TRUE, TRUE)) return?

Question 5 of 5

Which function correctly tests whether x is a character string?