1.Create a numeric variable “num_var” with the value “42.5”.
num_var <- 42.5
num_var
## [1] 42.5
2.Print the data type “char_var”.
char_var <- "R is fun!"
char_var
## [1] "R is fun!"
3.Print the data type “char_var”.
class(char_var)
## [1] "character"
4.Create a list “student_info” with the following elements:
“name”: “Mohammad Nasir Abdullah”
“age”: 18
“grades”: a numeric vector with values “99, 100, 89”.
student_info <- list( Name="Mohamad Nasir Abdullah", Age= 18, Grades= c(99,100,89))
student_info
## $Name
## [1] "Mohamad Nasir Abdullah"
##
## $Age
## [1] 18
##
## $Grades
## [1] 99 100 89
5.Create a data frame “df_students” with the following columns
1.”Name”: “John”, “Pablo”
2.”Age”: “22”, “30”
3.”Grade”: “A”, “C”
df_students <- list( Name= c("John","Pablo"), Age= c(22,30), Grade= c("A","C"))
df_students
## $Name
## [1] "John" "Pablo"
##
## $Age
## [1] 22 30
##
## $Grade
## [1] "A" "C"
6.Create a numeric vector “vec_num” with the values “5,10,15,20”.
vec_num <- c(5,10,15,20)
vec_num
## [1] 5 10 15 20
7.Extract the second and third elements from “vec_num”.
vec_num[c(2,3)]
## [1] 10 15
8.Create a vector “vec_seq” that contains a sequence of number from 1 to 10.
vec_seq <- seq(1,10, by=1)
vec_seq
## [1] 1 2 3 4 5 6 7 8 9 10
9.Create a vector “vec_rand” with 5 random numbers between 1 and 100.
set.seed(123456)
vec_rand <- sample(1:100, size = 5)
vec_rand
## [1] 60 42 71 54 74
10.Create a character vector “vec_char” with the values “apple”, “banana”, “cherry”.
vec_char <- c("apple","banana","cherry")
vec_char
## [1] "apple" "banana" "cherry"
11.Use the substr() function to extract the first 3 characters from each elements of “vec_char”.
substr(vec_char, start = 1, stop = 3)
## [1] "app" "ban" "che"
12.Create a character vector “vec_colors” with the values “red”, “blue”, “green”, “red”, “blue”.
vec_colors <- c("red","blue","green","red","blue")
13.Convert “vec_colors” into a factor vector “factor_colors”.
factor_colors <- as.factor(vec_colors)
14.Print the levels of “factor_colors”.
factor_colors
## [1] red blue green red blue
## Levels: blue green red
15.Given a numeric vector “vec_bonus = c(10,20,30,40,50)”, write a code snippet to :
vec_bonus <- c(10,20,30,40,50)
1.Extract all elements greater than 25.
vec_bonus[vec_bonus>25]
## [1] 30 40 50
2.Calculate the mean of the extracted elements.
mean(vec_bonus[vec_bonus>25])
## [1] 40
16.Given the following vectors:
weights <- c(55, 68, 72, 61, 58, 75, 64, 70) names <- c(“Alice”, “Bob”, “Charlie”, “Daisy”, “Eve”, “Frank”, “Grace”, “Henry”)
weights <- c(55, 68, 72, 61, 58, 75, 64, 70)
names <- c("Alice", "Bob","Charlie","Daisy","Eve","Frank","Grace","Henry")
names(weights) <- names
weights
## Alice Bob Charlie Daisy Eve Frank Grace Henry
## 55 68 72 61 58 75 64 70
weights[weights > 65]
## Bob Charlie Frank Henry
## 68 72 75 70
mean(weights[weights <= 60])
## [1] 56.5
weights[startsWith(names,"A")| startsWith(names,"D")]
## Alice Daisy
## 55 61
length(weights[weights>= 60 & weights <= 70])
## [1] 4
17.Given the following vector of daily temperatures (in Celsius) for a week:
temperatures <- c(22, 25, 19, 21, 18, 24, 23) days <- c(“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, “Sunday”)
temperatures <- c(22,25,19,21,18,24,23)
days <- c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")
names(temperatures) <- days
temperatures
## Monday Tuesday Wednesday Thursday Friday Saturday Sunday
## 22 25 19 21 18 24 23
temperatures[temperatures<20]
## Wednesday Friday
## 19 18
max(temperatures) - min(temperatures)
## [1] 7
length(temperatures[temperatures >= 20 & temperatures <= 24])
## [1] 4
18.Given the following vector of exam scores:
scores <- c(85, 78, 92, 65, 88, 70, 95, 80, 60, 90) students <- c(“Anna”, “Ben”, “Cara”, “Dan”, “Ella”, “Finn”, “Grace”, “Hank”, “Ivy”, “Jack”)
scores <- c(85,78,92,65,88,70,95,80,60,90)
students <- c("Anna", "Ben", "Cara", "Dan", "Ella", "Finn", "Grace", "Hank", "Ivy", "Jack")
names(scores) <- students
scores[scores > 90]
## Cara Grace
## 92 95
mean(scores[scores <80])
## [1] 68.25
scores[startsWith(students,"A")|startsWith(students,"E")|startsWith(students,"I")| startsWith(students,"O")|startsWith(students,"U")]
## Anna Ella Ivy
## 85 88 60
Exercise 1: Basic Matrix Creation
1.Create a matrix with numbers from 1 to 12, having 4 rows. Print the matrix.
matrix(1:12, nrow=4 )
## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
2.Now, reshape the same data into a matrix with 3 columns. Print the result.
matrix(1:12, ncol=3)
## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
Exercise 2: Filling Order
1.Create a matrix with numbers from 1 to 6, having 2 rows, filled by columns. Print the matrix.
matrix(1:6, nrow=2, byrow= FALSE)
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
2.Create another matrix with the same data, but this time filled by rows. Print the matrix and compare with the previous one.
matrix(1:6, nrow=2 , byrow= TRUE)
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
Exercise 3: Naming Matrix Dimensions
1.Create a matrix with numbers from 1 to 9, having 3 rows.
matrix(1:9, nrow=3)
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
2.Assign row names as “R1”, “R2”, and “R3”. Assign column names as “C1”, “C2”, and “C3”.
rownames <- c("R1", "R2", "R3")
columnnames <- c("C1", "C2", "C3")
3.Print the matrix with row and column names.
matrix(1:9, nrow=3, ncol=3, byrow= FALSE,
dimnames = list(rownames, columnnames))
## C1 C2 C3
## R1 1 4 7
## R2 2 5 8
## R3 3 6 9
Exercise 4: Accessing Matrix Elements
1.Consider the matrix:
mat <- matrix(1:9, nrow=3, dimnames=list(c("R1", "R2", "R3"), c("C1", "C2", "C3")))
mat
## C1 C2 C3
## R1 1 4 7
## R2 2 5 8
## R3 3 6 9
mat[2,3]
## [1] 8
mat[2, ]
## C1 C2 C3
## 2 5 8
c.Print the entire first column.
mat[ ,1]
## R1 R2 R3
## 1 2 3
Exercise 5: Matrix Operations
1.Create two matrices:
A <- matrix(1:4, nrow=2)
B <- matrix(5:8, nrow=2)
A;B
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
## [,1] [,2]
## [1,] 5 7
## [2,] 6 8
A+B
## [,1] [,2]
## [1,] 6 10
## [2,] 8 12
A*B
## [,1] [,2]
## [1,] 5 21
## [2,] 12 32
Exercise 6: Advanced Matrix Creation
1.Create a diagonal matrix with the numbers 4, 5, and 6 on its diagonal. Print the matrix.
x<- diag(x=c(4,5,6), nrow=3, ncol=3, names= T)
x
## [,1] [,2] [,3]
## [1,] 4 0 0
## [2,] 0 5 0
## [3,] 0 0 6
2.Check if the matrix from the previous step is symmetric. Print the result.
isSymmetric(x)
## [1] TRUE
Exercise 7: Basic Matrix Operations
1.Create two 2x2 matrices: A=[2435] B=[1324]
A <- matrix(2:5, nrow=2, ncol=2, byrow= TRUE)
B <- matrix(1:4, nrow=2, ncol=2, byrow= TRUE)
A;B
## [,1] [,2]
## [1,] 2 3
## [2,] 4 5
## [,1] [,2]
## [1,] 1 2
## [2,] 3 4
A+B
## [,1] [,2]
## [1,] 3 5
## [2,] 7 9
A-B
## [,1] [,2]
## [1,] 1 1
## [2,] 1 1
Exercise 8: Matrix Multiplication
1.Using the matrices A and B from Exercise 7:
A*B
## [,1] [,2]
## [1,] 2 6
## [2,] 12 20
A%%B
## [,1] [,2]
## [1,] 0 1
## [2,] 1 1
Exercise 9: Scalar Operations
1.For the matrix A from Exercise 7:
A*3
## [,1] [,2]
## [1,] 6 9
## [2,] 12 15
A/2
## [,1] [,2]
## [1,] 1 1.5
## [2,] 2 2.5
Exercise 10: Transposition and Inversion
1.Using the matrix A from Exercise 7:
t(A)
## [,1] [,2]
## [1,] 2 4
## [2,] 3 5
solve(A)
## [,1] [,2]
## [1,] -2.5 1.5
## [2,] 2.0 -1.0
Exercise 11: Diagonal and Power Operations
1.For the matrix A from Exercise 7:
diag(A)
## [1] 2 5
A^3
## [,1] [,2]
## [1,] 8 27
## [2,] 64 125
Exercise 12: Advanced Operations
1.Create a matrix C
C <- matrix(1:9, nrow=3, ncol=3, byrow= TRUE)
C
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [3,] 7 8 9
#A*C
#It cannot be multiplied since the number of columns in A is not equal to the number of rows in C
C[2,3]
## [1] 6
Exercise 13: Basic Matrix Indexing
1.Create the following matrix:
Matrix M:
M <- matrix(1:9, nrow=3,ncol=3)
M
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
M[3,1]
## [1] 3
M[2, ]
## [1] 2 5 8
M[ ,3]
## [1] 7 8 9
Exercise 14: Advanced Matrix Indexing
1.Using the matrix M from Exercise 13:
M[c(1,3), 2]
## [1] 4 6
M[c(1,2), c(1,3)]
## [,1] [,2]
## [1,] 1 7
## [2,] 2 8
M[M>4]
## [1] 5 6 7 8 9
Exercise 15: Conditional Indexing
1.Still with matrix M:
M[M%%2==0]
## [1] 2 4 6 8
M[M<7 & M%%2!=0]
## [1] 1 3 5
Exercise 16: Negative Indexing
1.Once again, with matrix M:
M[-2, ]
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 3 6 9
M[,c(-1,-3)]
## [1] 4 5 6
Exercise 17: Practical Application
1.Imagine matrix M represents scores of 3 students in 3 subjects. Rows represent students and columns represent subjects.
M[,3]
## [1] 7 8 9
M[2,1] <- 5
M
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 5 5 8
## [3,] 3 6 9
mean(M[1, ])
## [1] 4
Exercise 1: Basic Array Operations
1.Create a 3D array named my_array with dimensions 3x4x2 using numbers from 1 to 24. Print the array.
my_array <- array(1:24, c(3,4,2))
my_array
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 1 4 7 10
## [2,] 2 5 8 11
## [3,] 3 6 9 12
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 13 16 19 22
## [2,] 14 17 20 23
## [3,] 15 18 21 24
2.Access and print the element located in the 2nd row, 3rd column, and 1st layer of my_array.
my_array[2,3,1]
## [1] 8
3.Retrieve the entire 1st layer of my_array. What values are present?
my_array[ , ,1]
## [,1] [,2] [,3] [,4]
## [1,] 1 4 7 10
## [2,] 2 5 8 11
## [3,] 3 6 9 12
Exercise 2: Array Arithmetic
1.Create another 3D array named another_array with dimensions 3x4x2 using numbers from 25 to 48.
another_array <- array(25:48, c(3,4,2))
another_array
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 25 28 31 34
## [2,] 26 29 32 35
## [3,] 27 30 33 36
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 37 40 43 46
## [2,] 38 41 44 47
## [3,] 39 42 45 48
2.Perform and print the result of the element-wise addition of my_array and another_array.
my_array + another_array
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 26 32 38 44
## [2,] 28 34 40 46
## [3,] 30 36 42 48
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 50 56 62 68
## [2,] 52 58 64 70
## [3,] 54 60 66 72
3.Multiply my_array by a scalar value of 2. Print the result.
my_array*2
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 2 8 14 20
## [2,] 4 10 16 22
## [3,] 6 12 18 24
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 26 32 38 44
## [2,] 28 34 40 46
## [3,] 30 36 42 48
4.Execute element-wise multiplication between my_array and another_array. Print the outcome.
my_array*another_array
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 25 112 217 340
## [2,] 52 145 256 385
## [3,] 81 180 297 432
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 481 640 817 1012
## [2,] 532 697 880 1081
## [3,] 585 756 945 1152
Exercise 3: Using apply() with Arrays
1.Using my_array from Exercise 1:
apply(my_array, MARGIN= 1, sum)
## [1] 92 100 108
apply(my_array, MARGIN= 3, mean)
## [1] 6.5 18.5
c.Determine the maximum value for each column across all layers. Print the results.
apply(my_array, MARGIN=c(2,3), max)
## [,1] [,2]
## [1,] 3 15
## [2,] 6 18
## [3,] 9 21
## [4,] 12 24
2.Define a function that calculates the range (difference between maximum and minimum) of a numeric vector.
range_function <- function(x) {return(max(x)-min(x))}
apply(my_array, MARGIN= 3, range_function )
## [1] 11 11
mean_range <- function(x) {return (c(max(x)-min(x), mean(x)))}
apply(my_array, MARGIN= c(1,3), FUN=mean_range)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 9.0 9.0 9.0
## [2,] 5.5 6.5 7.5
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 9.0 9.0 9.0
## [2,] 17.5 18.5 19.5
Exercise 4: Advanced apply() Usage
1.Using my_array:
scalar_function <- function(x,k,y) {return (x*k+y)}
apply(my_array,MARGIN=3, scalar_function,k=0.5, y=10)
## [,1] [,2]
## [1,] 10.5 16.5
## [2,] 11.0 17.0
## [3,] 11.5 17.5
## [4,] 12.0 18.0
## [5,] 12.5 18.5
## [6,] 13.0 19.0
## [7,] 13.5 19.5
## [8,] 14.0 20.0
## [9,] 14.5 20.5
## [10,] 15.0 21.0
## [11,] 15.5 21.5
## [12,] 16.0 22.0
2.Calculate the standard deviation for each row in my_array across all columns and layers.
apply(my_array, MARGIN= c(2,3), sd)
## [,1] [,2]
## [1,] 1 1
## [2,] 1 1
## [3,] 1 1
## [4,] 1 1