num_var <- 42.5
char_var <- "R is fun!"
char_var
## [1] "R is fun!"
“name”: “Mohammad Nasir Abdullah”
“age”: 18
“grades”: a numeric vector with values “99, 100, 89”
student_info <- list(name = "Mohammad Nasir Abdullah",
age = 18,
grades = c(99, 100,89))
“Name”: “John”, “Pablo”
“Age”: “22”, “30”
“Grade”: “A”, “C”
df_students <- data.frame(Name = c("John", "Pablo"),
Age = c(22, 30),
Grade = c("A", "C"))
vec_num <- c(5, 10, 15, 20)
vec_num[c(2, 3)]
## [1] 10 15
vec_seq <- seq(10)
vec_rand <- runif(n = 5, min = 1, max = 100)
vec_rand
## [1] 96.21443 73.03983 85.30087 45.60498 94.65182
vec_char <- c("apple", "banana", "cherry")
substr(vec_char, start = 1, stop = 3)
## [1] "app" "ban" "che"
vec_colors <- c("blue", "green", "red", "blue")
factor_colors <- as.factor(vec_colors)
factor_colors
## [1] blue green red blue
## Levels: blue green red
vec_bonus <- c(10, 20, 30, 40, 50)
vec_bonus[vec_bonus > 25]
## [1] 30 40 50
mean(c(vec_bonus[vec_bonus > 25]))
## [1] 40
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[weights >= 60 & weights <= 70]])
## [1] 4
temperature <- c (22, 25, 19, 21, 18, 24,23)
days <- c("Mondays", "Tuesdays", "Wednesday", "Thursday", "Friday")
temperature_below20 <- days[temperature < 20]
temperature_below20
## [1] "Wednesday" "Friday"
difftemp <- max(temperature) - min(temperature)
difftemp
## [1] 7
length(days[temperature >= 20 & temperature <= 24])
## [1] 4
scores <- c(85, 78,92, 65, 88, 70, 95, 80, 60, 90)
students <- c("Anna", "Ben", "Cara", "Dan", "Ella", "Finn", "Grace", "Hank", "Ivy", "Jack")
students[scores > 90]
## [1] "Cara" "Grace"
mean(scores < 80)
## [1] 0.4
scores[grep(("^A|^E|^I|^O|^U"), students)]
## [1] 85 88 60
QUESTION 1
matrix(1:12, nrow = 4)
## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
matrix(1:12, nrow = 4, ncol = 3)
## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
QUESTION 2
matrix(1:6, nrow = 2)
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
matrix1 <- matrix(1:6, nrow = 2)
matrix2 <- matrix(1:6, nrow = 2, byrow = T)
matrix1;matrix2
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
QUESTION 3
matrix(1:9, nrow = 3)
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
rownames <- c("R1", "R2", "R3")
columnnames <- c("C1", "C2","C3")
matrix (1:9, nrow = 3, ncol = 3, byrow = F,
dimnames = list(rownames,columnnames))
## C1 C2 C3
## R1 1 4 7
## R2 2 5 8
## R3 3 6 9
QUESTION 4
mat <- matrix(1:9, nrow=3, dimnames=list(c("R1", "R2", "R3"), c("C1", "C2", "C3")))
mat[2,3]
## [1] 8
mat[2, ]
## C1 C2 C3
## 2 5 8
mat[ , 1]
## R1 R2 R3
## 1 2 3
QUESTION 5
A <- matrix(1:4, nrow=2)
B <- matrix(5:8, nrow=2)
A + B
## [,1] [,2]
## [1,] 6 10
## [2,] 8 12
A * B
## [,1] [,2]
## [1,] 5 21
## [2,] 12 32
QUESTION 6
diag(c(4,5,6))
## [,1] [,2] [,3]
## [1,] 4 0 0
## [2,] 0 5 0
## [3,] 0 0 6
isSymmetric.matrix(matrix1)
## [1] FALSE
QUESTION 7
A <- matrix(c(2,4,3,5), nrow = 2, ncol = 2, byrow = F)
B <- matrix(c(1,3,2,4), nrow = 2, ncol = 2, byrow = F)
A;B
## [,1] [,2]
## [1,] 2 3
## [2,] 4 5
## [,1] [,2]
## [1,] 1 2
## [2,] 3 4
A - B
## [,1] [,2]
## [1,] 1 1
## [2,] 1 1
QUESTION 8
A * B
## [,1] [,2]
## [1,] 2 6
## [2,] 12 20
A %*% B
## [,1] [,2]
## [1,] 11 16
## [2,] 19 28
QUESTION 9
A * 3
## [,1] [,2]
## [1,] 6 9
## [2,] 12 15
A / 2
## [,1] [,2]
## [1,] 1 1.5
## [2,] 2 2.5
QUESTION 10
t(A)
## [,1] [,2]
## [1,] 2 4
## [2,] 3 5
det(A)
## [1] -2
solve(A)
## [,1] [,2]
## [1,] -2.5 1.5
## [2,] 2.0 -1.0
QUESTION 11
diag(A)
## [1] 2 5
A * 3
## [,1] [,2]
## [1,] 6 9
## [2,] 12 15
QUESTION 12
C <- matrix(1:9, nrow = 3, ncol = 3, byrow = T)
#It is not possible because the dimension are not compatible.
C[2,3]
## [1] 6
QUESTION 13
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
QUESTION 14
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
QUESTION 15
M[M %% 2 == 0]
## [1] 2 4 6 8
M[M < 7 & M %% 2 == 1]
## [1] 1 3 5
QUESTION 16
M[-2, ]
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 3 6 9
M[ ,c(-1,-3)]
## [1] 4 5 6
QUESTION 17
M[ ,3]
## [1] 7 8 9
M[M==2] <- 5
M
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 5 5 8
## [3,] 3 6 9
mean(M[1, ])
## [1] 4
QUESTION 1
my_array <- array(1:24, dim = 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
my_array[2,3,1]
## [1] 8
my_array[ , ,1]
## [,1] [,2] [,3] [,4]
## [1,] 1 4 7 10
## [2,] 2 5 8 11
## [3,] 3 6 9 12
QUESTION 2
another_array <- array(25:48, dim = c(3,4,2))
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
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
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
QUESTION 3
apply(my_array, MARGIN = 1, sum)
## [1] 92 100 108
apply(my_array, MARGIN = 3, mean)
## [1] 6.5 18.5
apply(my_array, MARGIN = 3, max)
## [1] 12 24
range_function <- function(my_array) {
return(max(my_array) - min(my_array))
}
column_ranges <- apply(my_array, MARGIN = 3, FUN=range_function)
print(column_ranges)
## [1] 11 11
range_function <- function(my_array) {
return(max(my_array) - min(my_array))
}
column_ranges <- apply(my_array, MARGIN = 3, FUN=range_function)
print(column_ranges)
## [1] 11 11
range_function <- function(my_array) {
return(max(my_array) - min(my_array))
}
column_ranges <- apply(my_array, MARGIN = 3, FUN = range_function)
each_mean <- apply(my_array, MARGIN = 3, FUN = mean)
print(c(column_ranges, each_mean))
## [1] 11.0 11.0 6.5 18.5
QUESTION 4
scalar_function <- function(x, k, y) {return (x*k+y)}
b.Use the apply() function to apply this custom function on my_array, choosing a multiplication scalar of 0.5 and an addition scalar of 10. Print the result.
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
apply(my_array, MARGIN = c(2,3), sd)
## [,1] [,2]
## [1,] 1 1
## [2,] 1 1
## [3,] 1 1
## [4,] 1 1