R — Data Types
R's type hierarchy
R has a simple but powerful type system:
- Atomic types — the basic building blocks
- Vectors — collections of atomic values
- Lists — collections of anything
- Data frames — tabular data (covered later)
Everything in R is a vector — even single values are vectors of length 1.
Atomic types
| Type | Description | Example |
|---|---|---|
logical | TRUE/FALSE | TRUE, FALSE |
integer | Whole numbers | 42L, 100L |
double | Decimal numbers | 3.14, 2.718 |
character | Text strings | "hello", 'world' |
complex | Complex numbers | 3+4i |
raw | Raw bytes | as.raw(42) |
x <- 42 # double by default
y <- 42L # explicit integer
z <- "hello" # character
w <- TRUE # logical
Vectors — R's fundamental data structure
A vector is an ordered collection of values of the same type:
# Numeric vector
numbers <- c(1, 2, 3, 4, 5)
# Character vector
fruits <- c("apple", "banana", "cherry")
# Logical vector
flags <- c(TRUE, FALSE, TRUE, TRUE)
# Empty vector
empty <- c()
Use c() to combine values into a vector.
Vector operations
R operates on vectors element-wise:
x <- c(1, 2, 3, 4, 5)
y <- c(10, 20, 30, 40, 50)
x + y # 11 22 33 44 55
x * y # 10 40 90 160 250
x^2 # 1 4 9 16 25
sqrt(x) # 1.00 1.41 1.73 2.00 2.24
No loops needed — R applies the operation to every element automatically.
Sequences
# Using :
seq(1, 10) # 1 2 3 4 5 6 7 8 9 10
seq(1, 10, by = 2) # 1 3 5 7 9
seq(1, 10, length.out = 5) # 1.0 3.25 5.50 7.75 10.00
# Using rep
rep(1, 5) # 1 1 1 1 1
rep(c(1, 2), 3) # 1 2 1 2 1 2
rep(1:3, each = 2) # 1 1 2 2 3 3
Subsetting vectors
x <- c(10, 20, 30, 40, 50)
# By index
x[1] # 10 (first element)
x[c(1, 3)] # 10 30 (first and third)
x[-1] # 20 30 40 50 (all except first)
x[2:4] # 20 30 40 (second to fourth)
# By logical vector
x[c(TRUE, FALSE, TRUE, FALSE, TRUE)] # 10 30 50
# By condition
x[x > 25] # 30 40 50
Vector length
x <- c(1, 2, 3, 4, 5)
length(x) # 5
# Check if empty
length(x) == 0 # FALSE
Named vectors
scores <- c(Alice = 90, Bob = 85, Charlie = 95)
print(scores)
# Alice Bob Charlie
# 90 85 95
scores["Alice"] # 90
names(scores) # "Alice" "Bob" "Charlie"
Type checking and conversion
x <- c(1, 2, 3)
class(x) # "numeric"
typeof(x) # "double"
is.numeric(x) # TRUE
is.character(x) # FALSE
# Conversion
as.character(x) # "1" "2" "3"
as.numeric(c("1", "2", "3")) # 1 2 3
as.logical(c(0, 1, 0)) # FALSE TRUE FALSE
Matrices
A matrix is a 2D vector — rows and columns of the same type:
# Create a matrix
m <- matrix(1:12, nrow = 3, ncol = 4)
print(m)
# [,1] [,2] [,3] [,4]
# [1,] 1 4 7 10
# [2,] 2 5 8 11
# [3,] 3 6 9 12
# Access elements
m[1, 2] # 4 (row 1, column 2)
m[, 1] # 1 2 3 (all rows, column 1)
m[2, ] # 2 5 8 11 (row 2, all columns)
# Matrix operations
t(m) # transpose
m %*% t(m) # matrix multiplication
solve(m[, 1:3]) # inverse (if square and invertible)
Lists — mixed types
Lists can hold different types:
person <- list(
name = "Ada",
age = 36,
scores = c(90, 85, 95),
active = TRUE
)
# Access by name
person$name # "Ada"
person[["age"]] # 36
# Access by index
person[[1]] # "Ada"
person[[3]] # 90 85 95
# List structure
str(person)
Factors — categorical data
grades <- factor(c("A", "B", "A", "C", "B", "A"))
print(grades)
# A B A C B A
# Levels: A B C
table(grades)
# grades
# A B C
# 3 2 1
levels(grades) # "A" "B" "C"
Dates and times
# Date
today <- Sys.Date()
class(today) # "Date"
format(today, "%Y-%m-%d")
# POSIXct — date and time
now <- Sys.time()
class(now) # "POSIXct" "POSIXt"
# Parse from string
as.Date("2024-01-15")
as.POSIXct("2024-01-15 14:30:00")
Missing values
# NA — missing value
x <- c(1, 2, NA, 4, NA)
is.na(x) # FALSE FALSE TRUE FALSE TRUE
na.omit(x) # removes NAs
x[!is.na(x)] # 1 2 4
# Functions with na.rm
mean(x) # NA
mean(x, na.rm = TRUE) # 2.33
sum(x, na.rm = TRUE) # 7
Mini Practice
- Create vectors of each atomic type and check their classes
- Create a numeric vector 1-20 and extract every third element
- Build a matrix from 1-20 with 4 rows and extract the second column
- Create a list with mixed types and access each element by name
- Create a factor representing days of the week and count occurrences with
table()
Next: working with operators →
Related Topics
Frequently Asked Questions about Data Types
What is Data Types in R?
Data Types is a fundamental concept in R. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Data Types?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Data Types.
Why is Data Types important in R?
Data Types is essential for R development. Understanding this concept will help you write better code and solve real-world problems more effectively.