This document provides an overview of common data structures in R, including vectors, matrices, data frames, lists, factors, and arrays.
Vectors are one-dimensional arrays that can hold elements of the same data type.
# Numeric vector
num_vec <- c(1, 2, 3, 4, 5)
print(num_vec)
## [1] 1 2 3 4 5
# Character vector
char_vec <- c("apple", "banana", "cherry")
print(char_vec)
## [1] "apple" "banana" "cherry"
# Logical vector
log_vec <- c(TRUE, FALSE, TRUE, TRUE)
print(log_vec)
## [1] TRUE FALSE TRUE TRUE
# Using the : operator for a sequence
seq_vec <- 1:10
print(seq_vec)
## [1] 1 2 3 4 5 6 7 8 9 10
Matrices are 2D structures with rows and columns.
# Create a 3x3 matrix
mat <- matrix(1:9, nrow = 3, ncol = 3)
print(mat)
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
Data frames are used for storing tabular data.
# Create a data frame
df <- data.frame(
name = c("Alice", "Bob", "Charlie"),
age = c(25, 30, 35),
height = c(165, 180, 175)
)
print(df)
## name age height
## 1 Alice 25 165
## 2 Bob 30 180
## 3 Charlie 35 175
Lists can contain elements of different types.
# Create a list
my_list <- list(
numbers = 1:5,
text = "Hello, world!",
matrix = matrix(1:4, nrow = 2)
)
print(my_list)
## $numbers
## [1] 1 2 3 4 5
##
## $text
## [1] "Hello, world!"
##
## $matrix
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
Factors are used for categorical data.
# Create a factor
colors <- factor(c("red", "blue", "green", "red", "green"))
print(colors)
## [1] red blue green red green
## Levels: blue green red
print(levels(colors))
## [1] "blue" "green" "red"
Arrays are multi-dimensional structures.
# Create a 2x3x2 array
arr <- array(1:12, dim = c(2, 3, 2))
print(arr)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 7 9 11
## [2,] 8 10 12
These are the basic data structures in R. Each has its own use cases:
Understanding these structures is crucial for effective data manipulation and analysis in R.