The notebook contains code execution and assignment practices for my R journey in R Markdown

Learning Resource: Book of R and Data Camp data scientist with R certification track

1. Getting Started with R

1.1 Basic R Operations

# getting and setting working directory (the folder path where R is looking our for absolute loading and saving of files to)
getwd()
## [1] "C:/Users/MUSAAB-TECH/OneDrive/icammada/PROJECT"
setwd("C:/Users/MUSAAB-TECH/OneDrive/Documents")
getwd() # to confirm 
## [1] "C:/Users/MUSAAB-TECH/OneDrive/Documents"

simple arithmetic operations in R:

# Basic arithmetic operations
5 + 3
## [1] 8
10 - 4
## [1] 6
6 * 7
## [1] 42
20 / 4
## [1] 5

Exercise 1.1: Calculate the following expressions:

# Calculate 15 + 25
15 + 25
## [1] 40
# Calculate 100 - 37
100 - 37
## [1] 63
# Calculate 8 * 9
8 * 9
## [1] 72

1.2 Assignment and Objects

In R, we can store values in objects using the assignment operator <-:

# Assigning values to objects
x <- 10
y <- 5
result <- x + y
print(result)
## [1] 15

Exercise 1.2: Create my own variables:

# Create a variable 'age' with your age
age <- 25

# Create a variable 'name' with your name
name <- "Student"

# Print both variables
print(age)
## [1] 25
print(name)
## [1] "Student"

2. Vectors: The Foundation of R

Vectors are the most basic data structure in R. They contain elements of the same type of 1 dimension array.

2.1 Creating Vectors

# Creating numeric vectors
numbers <- c(1, 2, 3, 4, 5)
print(numbers)
## [1] 1 2 3 4 5
# Creating character vectors
fruits <- c("apple", "banana", "orange")
print(fruits)
## [1] "apple"  "banana" "orange"
# Creating logical vectors
logical_vals <- c(TRUE, FALSE, TRUE)
print(logical_vals)
## [1]  TRUE FALSE  TRUE

Exercise 2.1 : Create your own vectors:

# Create a vector of your favorite numbers
fav_numbers <- c(7, 13, 21, 42)
print(fav_numbers)
## [1]  7 13 21 42
# Create a vector of colors
colors <- c("red", "blue", "green", "yellow")
print(colors)
## [1] "red"    "blue"   "green"  "yellow"

2.2 Vector Operations and Functions

# Vector arithmetic
vec1 <- c(1, 2, 3, 4)
vec2 <- c(5, 6, 7, 8)

# Element-wise operations
sum_vec <- vec1 + vec2
print(sum_vec)
## [1]  6  8 10 12
# Vector functions
length(vec1)
## [1] 4
sum(vec1)
## [1] 10
mean(vec1)
## [1] 2.5

Exercise 2.2 : Practice vector operations:

# Create two vectors
scores1 <- c(85, 90, 78, 92)
scores2 <- c(88, 85, 82, 95)

# Calculate the average of both vectors
avg_scores <- (scores1 + scores2) / 2
print(avg_scores)
## [1] 86.5 87.5 80.0 93.5
# Find the maximum score in scores1
max(scores1)
## [1] 92

2.3 Sequences and Repetition

# Creating sequences
seq1 <- 1:10
print(seq1)
##  [1]  1  2  3  4  5  6  7  8  9 10
# Using seq() function
seq2 <- seq(0, 20, by = 2)
print(seq2)
##  [1]  0  2  4  6  8 10 12 14 16 18 20
# Repetition with rep()
rep_vec <- rep(c(1, 2, 3), times = 3)
print(rep_vec)
## [1] 1 2 3 1 2 3 1 2 3

Exercise 2.3 : Create complex sequences:

# Create a sequence from 5 to 50 by steps of 5
seq_5_50 <- seq(5, 50, by = 5)
print(seq_5_50)
##  [1]  5 10 15 20 25 30 35 40 45 50
# Repeat the pattern c("A", "B") 4 times
pattern <- rep(c("A", "B"), times = 4)
print(pattern)
## [1] "A" "B" "A" "B" "A" "B" "A" "B"
# Create a decreasing sequence from 100 to 1 by steps of -10
decreasing <- seq(100, 1, by = -10)
print(decreasing)
##  [1] 100  90  80  70  60  50  40  30  20  10

2.4 Vector Subsetting

Subsetting is crucial for data manipulation:

# Create a sample vector
sample_vec <- c(10, 20, 30, 40, 50)

# Access single elements
first_element <- sample_vec[1]
print(first_element)
## [1] 10
# Access multiple elements
subset1 <- sample_vec[c(1, 3, 5)]
print(subset1)
## [1] 10 30 50
# Access ranges
subset2 <- sample_vec[2:4]
print(subset2)
## [1] 20 30 40

Exercise 2.4 : Practice subsetting:

# Create a vector of months
months <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun")

# Extract the first three months
first_quarter <- months[1:3]
print(first_quarter)
## [1] "Jan" "Feb" "Mar"
# Extract specific months (1st, 4th, and 6th)
specific_months <- months[c(1, 4, 6)]
print(specific_months)
## [1] "Jan" "Apr" "Jun"

Exercise 2.5 : Advanced subsetting with conditions:

# Create a vector of test scores
test_scores <- c(78, 85, 92, 67, 88, 95, 73, 89)

# Find scores greater than 80
high_scores <- test_scores[test_scores > 80]
print(high_scores)
## [1] 85 92 88 95 89
# Find positions of scores greater than 85
high_positions <- which(test_scores > 85)
print(high_positions)
## [1] 3 5 6 8
# Replace scores below 75 with 75
adjusted_scores <- test_scores
adjusted_scores[adjusted_scores < 75] <- 75
print(adjusted_scores)
## [1] 78 85 92 75 88 95 75 89

Some Exercise in Book of R chapter two

Using R, verify that 6a + 42/3^4.2 - 3.62 = 29.50556 when a = 2.3.

# a. Verify that  6a + 42/3^4.2 - 3.62 = 29.50556 when a = 2.3.
a <- 2.3
result_a <- (6*a + 42) / 3^(4.2 - 3.62)
result_a
## [1] 29.50556
print(paste("Result:", result_a))
## [1] "Result: 29.5055606431273"
print(paste("Expected: 29.50556"))
## [1] "Expected: 29.50556"
print(paste("Match:", round(result_a, 5) == 29.50556))
## [1] "Match: TRUE"

a. Create an object that stores the value 32 × 41/8. Overwrite your object in (a) by itself divided by 2.33. Print the result to the console.

# a. Create object with 3^2 × 4^1/8
my_object <- 3^2 * 4^1/8
print(paste("Original value:", my_object))
## [1] "Original value: 4.5"
# b. Overwrite by dividing by 2.33
my_object <- my_object / 2.33
print(paste("After division by 2.33:", my_object))
## [1] "After division by 2.33: 1.931330472103"

3. Matrices and Arrays

Matrices are two-dimensional arrays of the same data type.

3.1 Creating Matrices

# Create a matrix using matrix() function
mat1 <- matrix(1:12, nrow = 3, ncol = 4)
print(mat1)
##      [,1] [,2] [,3] [,4]
## [1,]    1    4    7   10
## [2,]    2    5    8   11
## [3,]    3    6    9   12
# Create matrix by binding vectors
vec_a <- c(1, 2, 3)
vec_b <- c(4, 5, 6)
mat2 <- rbind(vec_a, vec_b)
print(mat2)
##       [,1] [,2] [,3]
## vec_a    1    2    3
## vec_b    4    5    6

Exercise 3.1 : Create your own matrices:

# Create a 2x3 matrix with numbers 1-6
my_matrix <- matrix(1:6, nrow = 2, ncol = 3)
print(my_matrix)
##      [,1] [,2] [,3]
## [1,]    1    3    5
## [2,]    2    4    6
# Create a matrix by column binding two vectors
col1 <- c(10, 20, 30)
col2 <- c(40, 50, 60)
combined_matrix <- cbind(col1, col2)
print(combined_matrix)
##      col1 col2
## [1,]   10   40
## [2,]   20   50
## [3,]   30   60

3.2 Matrix Operations

# Matrix dimensions
mat <- matrix(1:6, nrow = 2)
dim(mat)
## [1] 2 3
nrow(mat)
## [1] 2
ncol(mat)
## [1] 3
mat
##      [,1] [,2] [,3]
## [1,]    1    3    5
## [2,]    2    4    6
# Matrix transpose
t(mat)
##      [,1] [,2]
## [1,]    1    2
## [2,]    3    4
## [3,]    5    6
# Matrix multiplication
mat_a <- matrix(c(1, 2, 3, 4), nrow = 2)
mat_b <- matrix(c(5, 6, 7, 8), nrow = 2)
mat_product <- mat_a %*% mat_b
print(mat_product)
##      [,1] [,2]
## [1,]   23   31
## [2,]   34   46

Exercise 3.2 : Matrix calculations:

# Create two 3x3 matrices
matrix_x <- matrix(1:9, nrow = 3)
matrix_y <- matrix(9:1, nrow = 3)

print("Matrix X:")
## [1] "Matrix X:"
print(matrix_x)
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
print("Matrix Y:")
## [1] "Matrix Y:"
print(matrix_y)
##      [,1] [,2] [,3]
## [1,]    9    6    3
## [2,]    8    5    2
## [3,]    7    4    1
# Element-wise addition
addition <- matrix_x + matrix_y
print("Addition:")
## [1] "Addition:"
print(addition)
##      [,1] [,2] [,3]
## [1,]   10   10   10
## [2,]   10   10   10
## [3,]   10   10   10
# Matrix multiplication
multiplication <- matrix_x %*% matrix_y
print("Matrix multiplication:")
## [1] "Matrix multiplication:"
print(multiplication)
##      [,1] [,2] [,3]
## [1,]   90   54   18
## [2,]  114   69   24
## [3,]  138   84   30

3.3 Matrix Subsetting

# Create a sample matrix
sample_matrix <- matrix(1:12, nrow = 3, ncol = 4)
print(sample_matrix)
##      [,1] [,2] [,3] [,4]
## [1,]    1    4    7   10
## [2,]    2    5    8   11
## [3,]    3    6    9   12
# Access specific elements
element <- sample_matrix[2, 3]
print(element)
## [1] 8
# Access entire rows or columns
row2 <- sample_matrix[2, ]
col3 <- sample_matrix[, 3]
print(row2)
## [1]  2  5  8 11
print(col3)
## [1] 7 8 9

Exercise 3.3 : Practice matrix subsetting:

# Create a 4x4 matrix
big_matrix <- matrix(1:16, nrow = 4)
print(big_matrix)
##      [,1] [,2] [,3] [,4]
## [1,]    1    5    9   13
## [2,]    2    6   10   14
## [3,]    3    7   11   15
## [4,]    4    8   12   16
# Extract the diagonal elements
diagonal <- diag(big_matrix)
print(diagonal)
## [1]  1  6 11 16
# Extract a 2x2 submatrix from the top-left corner
submatrix <- big_matrix[1:2, 1:2]
print(submatrix)
##      [,1] [,2]
## [1,]    1    5
## [2,]    2    6

Some Exercise in Book of R- Chapter three

# Construct and store a 4 × 2 matrix that's filled row-wise with the values 4.3, 3.1, 8.2, 8.2, 3.2, 0.9, 1.6, and 6.5, in that order.

# Confirm the dimensions of the matrix from (a) are 3 × 2 if you remove the first row.

# a. Create 4x2 matrix filled row-wise
values <- c(4.3, 3.1, 8.2, 8.2, 3.2, 0.9, 1.6, 6.5)
my_matrix <- matrix(values, nrow = 4, ncol = 2, byrow = TRUE)
print("4x2 matrix:")
## [1] "4x2 matrix:"
print(my_matrix)
##      [,1] [,2]
## [1,]  4.3  3.1
## [2,]  8.2  8.2
## [3,]  3.2  0.9
## [4,]  1.6  6.5
# b. Remove first row and check dimensions
matrix_no_first_row <- my_matrix[-1, ]
print("Matrix after removing first row:")
## [1] "Matrix after removing first row:"
print(matrix_no_first_row)
##      [,1] [,2]
## [1,]  8.2  8.2
## [2,]  3.2  0.9
## [3,]  1.6  6.5
dim(matrix_no_first_row)
## [1] 3 2