num_var <- 42.5
num_var
## [1] 42.5
char_var <-"R is fun!"
char_var
## [1] "R is fun!"
print (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))
student_info
## $name
## [1] "Mohammad Nasir Abdullah"
##
## $age
## [1] 18
##
## $grades
## [1] 99 100 89
“Name”: “John”, “Pablo”
“Age”: “22”, “30”
“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"
vec_num <- c(5,10,15,20)
vec_num [c(2,3)]
## [1] 10 15
vec_seq <- seq (1:10)
vec_seq
## [1] 1 2 3 4 5 6 7 8 9 10
vec_rand <- seq (1,100 , by = 5)
vec_rand
## [1] 1 6 11 16 21 26 31 36 41 46 51 56 61 66 71 76 81 86 91 96
vec_char <- c("apple", "banana", "cherry")
vec_char
## [1] "apple" "banana" "cherry"
vec_char <- substr(vec_char, 1, 3)
vec_char
## [1] "app" "ban" "che"
vec_colors <- c("red", "blue", "green", "red", "blue")
vec_colors
## [1] "red" "blue" "green" "red" "blue"
factor_colors <- as.factor(vec_colors)
print (factor_colors)
## [1] red blue green red blue
## Levels: blue green red
vec_bonus <- c(10,20,30,40,50)
vec_bonus > 25
## [1] FALSE FALSE TRUE TRUE TRUE
mean (vec_bonus)
## [1] 30
weights <- c(55, 68, 72, 61, 58, 75, 64, 70)
names <- c("Alice", "Bob", "Charlie", "Daisy", "Eve", "Frank", "Grace", "Henry")
names <- (weights >65)
names
## [1] FALSE TRUE TRUE FALSE FALSE TRUE FALSE TRUE
mean (weights [weights <= 60])
## [1] 56.5
weights <- names [start= "A| D"]
weights
## [1] NA
length(weights[weights>= 60 & weights<= 70])
## [1] 1
temperatures <- c(22, 25, 19, 21, 18, 24, 23)
days <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
length (days[temperatures <20])
## [1] 2
max(temperatures)-min(temperatures)
## [1] 7
length (days [ temperatures >= 20 & temperatures <=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 [scores <80])
## [1] 68.25
scores[startsWith(students,"A")|startsWith(students,"E")|startsWith(students,"I")|
startsWith(students,"O")|startsWith(students,"U")]
## [1] 85 88 60
#TAKE HOME EXERCISE
numeric_vector <- c(10,11,12,13,14,15,16,17,18,19,20)
character_vector <- c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")
a <- c(1, 2, 3)
b <- c(4, 5, 6)
c(a,b)
## [1] 1 2 3 4 5 6
temperatures <- c(15, 18, 20, 22, 19, 17, 16)
a.mess <- temperatures
a.mess[c(2,4,6)]
## [1] 18 22 17
a.mess[5]
## [1] 19
{r}
seq (5,20, by=5)
## [1] 5 10 15 20
rep (6, times=10)
## [1] 6 6 6 6 6 6 6 6 6 6
fruits <- c("apple", "banana", "cherry")
substr (fruits, start=1 , stop=3)
## [1] "app" "ban" "che"
ages <- c(25, 30, 35, 40, 45, 50)
length (ages [ages > 30 & ages < 50])
## [1] 3
vector1 <- c(5, 10, 15)
vector2 <- c(10, 10, 20)
comparison_result <- (vector1<= vector2)
comparison_result
## [1] TRUE TRUE TRUE
#Exercise 1: Basic Matrix Creation
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, nrow=4, 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
matrix(1:6, nrow=2, byrow=FALSE)
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
matrix(1:6, nrow=2, byrow=TRUE)
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
Exercise 3: Naming Matrix Dimensions
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,
dimnames = list(rownames, columnnames))
## C1 C2 C3
## R1 1 4 7
## R2 2 5 8
## R3 3 6 9
Exercise 4: Accessing Matrix Elements
Consider the matrix:
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
Exercise 5: Matrix Operations
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,] 1 3
## [2,] 2 4
Exercise 6: Advanced Matrix Creation
I <- c(4,5,6)
diag(I)
## [,1] [,2] [,3]
## [1,] 4 0 0
## [2,] 0 5 0
## [3,] 0 0 6
isSymmetric.matrix(I)
## [1] FALSE
Exercise 7: Basic Matrix Operations
A <- matrix(c(2,3,4,5), nrow=2, ncol=2, byrow=TRUE)
B <- matrix(c(1,3,2,4), nrow=2, ncol=2)
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
A*B
## [,1] [,2]
## [1,] 2 6
## [2,] 12 20
A%%B
## [,1] [,2]
## [1,] 0 1
## [2,] 1 1
Exercise 9: Scalar Operations
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
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
Exercise 11: Diagonal and Power Operations
diag(A)
## [1] 2 5
A^3
## [,1] [,2]
## [1,] 8 27
## [2,] 64 125
Exercise 12: Advanced Operations
C<- matrix(1:9, nrow=3, ncol=3, byrow=TRUE)
print(C)
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [3,] 7 8 9
#is it not possible because non-comformable arrays.
#C<- c[2,3]
Exercise 13: Basic Matrix Indexing
Matrix M:
M<-matrix(1:9, nrow=3,ncol=3)
M[3,1]
## [1] 3
M[2,]
## [1] 2 5 8
M[,3]
## [1] 7 8 9
Exercise 14: Advanced Matrix Indexing
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
Still with matrix M:
M[M %% 2==0]
## [1] 2 4 6 8
M[M < 7 & M %%2==1]
## [1] 1 3 5
Exercise 16: Negative Indexing
Once again, with matrix M:
M[2,]
## [1] 2 5 8
M[,c(-1,-3)]
## [1] 4 5 6
Exercise 17: Practical Application
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[M==2] <- 5
mean(M[1,])
## [1] 4
Exercise 1: Basic Array Operations
data_vector <- 1:24
dim_vector <- c(3,4,2)
my_array <- array(data_vector, dim=dim_vector)
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
Exercise 2: Array Arithmetic
data_vector <- 25:48
dim_vector <- c(3,4,2)
another_array <- array(data_vector, dim=dim_vector)
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
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
Exercise 3: Using apply() with Arrays
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=c(2,3), max)
## [,1] [,2]
## [1,] 3 15
## [2,] 6 18
## [3,] 9 21
## [4,] 12 24
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
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
apply(my_array, MARGIN=c(2,3),sd)
## [,1] [,2]
## [1,] 1 1
## [2,] 1 1
## [3,] 1 1
## [4,] 1 1