R Programming notes
R Programming notes
Vectors
Vectors are a fundamental data structure in R used to store sequences of
elements that are all of the same type. They are essential for data
manipulation and statistical analysis.
Types of Vectors in R
1. Numeric Vector
c means concatenate
2. Character Vector
3. Logical Vector
4. Integer Vector
5. Complex Vector
print("Numeric Vector:")
print("Character Vector:")
print("Logical Vector:")
print("Integer Vector:")
print("Complex Vector:")
Vector Operations
Summary
Vectors are the simplest data structures in R and can only hold
elements of the same type.
1. Arithmetic Operations
Addition (+)
Subtraction (-)
Multiplication (*)
Division (/)
Exponentiation (^)
Example:
Copy code
# Element-wise addition
# Element-wise subtraction
# Element-wise multiplication
# Element-wise division
# Element-wise exponentiation
result_exp <- vec1 ^ vec2 # Outputs: [1] 2^1 4^3 6^5 -> [1] 2 64
7776
2. Relational Operations
Equal to (==)
Example:
Copy code
result_gt <- vec1 > vec2 # Outputs: [1] TRUE FALSE FALSE
# Equal to comparison
3. Logical Operations
These operations work with logical vectors and return logical results:
AND (&)
OR (|)
NOT (!)
Example:
Copy code
# Logical AND
result_and <- vec1 & vec2 # Outputs: [1] FALSE FALSE TRUE
# Logical OR
# Logical NOT
4. Vector Recycling
Example:
Copy code
Example:
Copy code
# Conditional access
Example:
Copy code
# Sum of elements
# Mean of elements
# Maximum element
Summary
Element access and functions like sum(), mean(), max(), and min()
are useful for vector manipulation and analysis.
4o
Examples:
Using the Colon Operator:
seq1 <- 1:10 # Creates a sequence from 1 to 10
print(seq1) # Outputs: [1] 1 2 3 4 5 6 7 8 9 10
4. Lengths
The length() function is used to determine the number of
elements in a vector or list.
Examples:
Finding the Length of a Vector:
vec <- c(4, 7, 1, 9, 3)
len <- length(vec) # Returns the number of elements in the
vector
print(len) # Outputs: 5
Matrices
# [3,] 7 8 9
Creating a Matrix by Combining Vectors:
vec1 <- c(1, 2, 3)
vec2 <- c(4, 5, 6)
combined_mat <- rbind(vec1, vec2) # Creates a matrix by
binding rows
print(combined_mat)
# Outputs:
# [,1] [,2] [,3]
# vec1 1 2 3
# vec2 4 5 6
2. Accessing Matrix Elements
You can access specific elements, rows, or columns using indices.
Examples:
Accessing a Specific Element:
mat <- matrix(1:9, nrow = 3)
elem <- mat[2, 3] # Extracts the element in the 2nd row, 3rd
column
print(elem) # Outputs: 8
Accessing Entire Rows or Columns:
row_extract <- mat[1, ] # Extracts the first row
print(row_extract) # Outputs: [1] 1 4 7
3. Matrix Operations
Matrices support various mathematical operations, including
addition, subtraction, multiplication, and transposition.
Examples:
Matrix Addition and Subtraction:
mat1 <- matrix(1:4, nrow = 2)
mat2 <- matrix(5:8, nrow = 2)
sum_mat <- mat1 + mat2
print(sum_mat)
# Outputs:
# [,1] [,2]
# [1,] 6 10
# [2,] 8 12
Matrix Multiplication: Use %*% for matrix multiplication.
mat_mult <- mat1 %*% mat2
print(mat_mult)
# Outputs (result depends on matrix dimensions):
# [,1] [,2]
# [1,] 19 22
# [2,] 43 50
Element-wise Multiplication: Use * for element-wise
multiplication.
elementwise_mult <- mat1 * mat2
print(elementwise_mult)
# Outputs:
# [,1] [,2]
# [1,] 5 21
# [2,] 12 32
Transposing a Matrix:
5. Combining Matrices
Summary
ARRAY
Arrays in R are multi-dimensional data structures that can hold
data of the same type (usually numeric or character). Unlike
matrices, which are limited to two dimensions, arrays can have
more than two dimensions, making them suitable for more
complex data structures.
1. Creating Arrays
You can create arrays in R using the array() function, specifying
the data and dimensions.
Syntax:
array(c(rows,columns,layers).
array(data, dim = c(dim1, dim2, dim3, ...))
data: The elements to populate the array.
dim: A vector specifying the number of elements in each
dimension.
Example:
#,,2
# [,1] [,2] [,3] [,4]
# [1,] 13 16 19 22
# [2,] 14 17 20 23
# [3,] 15 18 21 24
3. Modifying Elements
You can modify elements in an array by assigning new values to
specific positions.
Example:
arr_3d[2, 2, 1] <- 99 # Modifies the element at the 2nd row, 2nd
column, 1st matrix
print(arr_3d[2, , 1]) # Outputs the modified 2nd row of the 1st
matrix: [1] 2 99 8 11
4. Array Operations
Arrays support arithmetic operations, which are performed
element-wise.
Examples:
Addition:
arr1 <- array(1:6, dim = c(2, 3))
arr2 <- array(7:12, dim = c(2, 3))
arr_sum <- arr1 + arr2
print(arr_sum)
# Outputs:
# [,1] [,2] [,3]
# [1,] 8 12 16
# [2,] 10 14 18
Multiplication:
arr_mult <- arr1 * arr2
print(arr_mult)
# Outputs:
library(abind)
combined_arr <- abind(arr1, arr2, along = 3)
print(dim(combined_arr)) # Outputs: [1] 2 3 2 (2 rows, 3
columns, 2 matrices)
Summary
Arrays in R can be multi-dimensional, supporting data
storage beyond 2D matrices.
Creation is done with array() by specifying data and
dimensions.
Element Access uses indexing for each dimension.
Arithmetic Operations are element-wise and can involve
more than one array.
Properties can be checked using dim(), length(), and
dimnames().
when working with multi-dimensional datasets.