```## Question 1
n_dims <- sample(x=seq(3,10), size = 1)
n_dims^2
## [1] 49
z <- c(1:n_dims^2)
Sample_z <- sample(z)
# create a square matrix
my_m <- matrix(data=Sample_z, ncol=Sample_z, nrow = Sample_z)
print(my_m)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 7 5 33 39 1 49 45
## [2,] 29 37 6 31 2 10 23
## [3,] 43 30 42 25 22 18 4
## [4,] 16 11 38 24 44 20 32
## [5,] 46 26 21 35 40 47 36
## [6,] 41 15 19 13 34 28 27
## [7,] 48 12 17 8 3 9 14
# transpose matrix
t_my_m <- t(my_m)
print(t_my_m)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 7 29 43 16 46 41 48
## [2,] 5 37 30 11 26 15 12
## [3,] 33 6 42 38 21 19 17
## [4,] 39 31 25 24 35 13 8
## [5,] 1 2 22 44 40 34 3
## [6,] 49 10 18 20 47 28 9
## [7,] 45 23 4 32 36 27 14
# calculate sum and mean in 1st row
sum(t_my_m[1,])
## [1] 230
mean(t_my_m[1,])
## [1] 32.85714
# calculate sum and mean in 1st row
sum(t_my_m[5,])
## [1] 146
mean(t_my_m[5,])
## [1] 20.85714
eigen(my_m)
## eigen() decomposition
## $values
## [1] 174.235449+ 0.00000i -28.884243+12.46643i -28.884243-12.46643i
## [4] 26.842466+ 0.00000i 20.161913+16.17549i 20.161913-16.17549i
## [7] 8.366746+ 0.00000i
##
## $vectors
## [,1] [,2] [,3]
## [1,] 0.3561993+0i -0.55842212+0.00000000i -0.55842212+0.00000000i
## [2,] 0.2580708+0i 0.20735241+0.14213449i 0.20735241-0.14213449i
## [3,] 0.3993480+0i 0.30239434+0.09048274i 0.30239434-0.09048274i
## [4,] 0.4116376+0i -0.40988161-0.30782852i -0.40988161+0.30782852i
## [5,] 0.5338354+0i 0.09581621+0.08843452i 0.09581621-0.08843452i
## [6,] 0.3796050+0i 0.08452091-0.09306298i 0.08452091+0.09306298i
## [7,] 0.2202658+0i 0.46157382+0.12930736i 0.46157382-0.12930736i
## [,4] [,5] [,6] [,7]
## [1,] 0.06648827+0i -0.1588642-0.1716159i -0.1588642+0.1716159i 0.1317552+0i
## [2,] -0.67443659+0i -0.2141939+0.3953456i -0.2141939-0.3953456i -0.6219302+0i
## [3,] -0.28578508+0i 0.1704032-0.3256455i 0.1704032+0.3256455i -0.1007586+0i
## [4,] 0.28572930+0i 0.3293633-0.1950894i 0.3293633+0.1950894i 0.6753335+0i
## [5,] 0.46872295+0i 0.3122684+0.2783517i 0.3122684-0.2783517i 0.1994856+0i
## [6,] 0.29518473+0i 0.0888638+0.2300247i 0.0888638-0.2300247i -0.2286528+0i
## [7,] -0.26564078+0i -0.4750908+0.0000000i -0.4750908+0.0000000i -0.1937499+0i
# checking kind of numbers
typeof(eigen(my_m)$values)
## [1] "complex"
typeof(eigen(my_m)$vectors)
## [1] "complex"
# creating a 4*4 matrix
z <- runif(16)
my_matrix <- matrix(data=z, ncol=4)
# creating logical values
x <- runif(100)
my_logical <- x < 0.47
print(my_logical)
## [1] FALSE TRUE FALSE FALSE TRUE TRUE TRUE FALSE TRUE FALSE FALSE FALSE
## [13] TRUE FALSE FALSE TRUE TRUE TRUE TRUE FALSE FALSE FALSE TRUE TRUE
## [25] FALSE TRUE TRUE TRUE FALSE FALSE TRUE FALSE TRUE TRUE FALSE FALSE
## [37] FALSE FALSE TRUE TRUE TRUE TRUE FALSE TRUE FALSE FALSE TRUE FALSE
## [49] FALSE FALSE TRUE FALSE FALSE TRUE FALSE TRUE TRUE FALSE FALSE TRUE
## [61] TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE TRUE FALSE TRUE TRUE
## [73] TRUE TRUE TRUE FALSE TRUE TRUE FALSE TRUE TRUE FALSE FALSE TRUE
## [85] FALSE FALSE TRUE TRUE FALSE FALSE TRUE FALSE FALSE FALSE TRUE FALSE
## [97] FALSE TRUE TRUE FALSE
my_letters <- sample(letters)
newlist <- list(my_matrix[2,2], my_logical[2],my_letters[2])
typeof(newlist[1])
## [1] "list"
typeof(newlist[2])
## [1] "list"
typeof(newlist[3])
## [1] "list"
z <- c(0.7743394, TRUE,"S")
typeof(z)
## [1] "character"
my_unis<- seq(from=0, to=10, length=26)
my_letters <- sample(LETTERS)
#creating a dataframe
dframe<- data.frame (my_unis,my_letters)
print(dframe)
## my_unis my_letters
## 1 0.0 I
## 2 0.4 Q
## 3 0.8 U
## 4 1.2 X
## 5 1.6 A
## 6 2.0 W
## 7 2.4 L
## 8 2.8 O
## 9 3.2 E
## 10 3.6 P
## 11 4.0 S
## 12 4.4 N
## 13 4.8 F
## 14 5.2 K
## 15 5.6 Y
## 16 6.0 B
## 17 6.4 C
## 18 6.8 R
## 19 7.2 D
## 20 7.6 V
## 21 8.0 H
## 22 8.4 Z
## 23 8.8 M
## 24 9.2 G
## 25 9.6 T
## 26 10.0 J
# adding NAs randomly to my_unis column
dframe[sample(length(dframe$my_unis), size = 4),1]<- NA
print(dframe)
## my_unis my_letters
## 1 0.0 I
## 2 0.4 Q
## 3 0.8 U
## 4 1.2 X
## 5 1.6 A
## 6 NA W
## 7 NA L
## 8 2.8 O
## 9 3.2 E
## 10 3.6 P
## 11 4.0 S
## 12 NA N
## 13 4.8 F
## 14 5.2 K
## 15 5.6 Y
## 16 6.0 B
## 17 6.4 C
## 18 6.8 R
## 19 7.2 D
## 20 7.6 V
## 21 8.0 H
## 22 NA Z
## 23 8.8 M
## 24 9.2 G
## 25 9.6 T
## 26 10.0 J
# rearranging my_letters in alphabetical order
dframe[order(dframe$my_letters),]
## my_unis my_letters
## 5 1.6 A
## 16 6.0 B
## 17 6.4 C
## 19 7.2 D
## 9 3.2 E
## 13 4.8 F
## 24 9.2 G
## 21 8.0 H
## 1 0.0 I
## 26 10.0 J
## 14 5.2 K
## 7 NA L
## 23 8.8 M
## 12 NA N
## 8 2.8 O
## 10 3.6 P
## 2 0.4 Q
## 18 6.8 R
## 11 4.0 S
## 25 9.6 T
## 3 0.8 U
## 20 7.6 V
## 6 NA W
## 4 1.2 X
## 15 5.6 Y
## 22 NA Z