4.2.1 Create an array (3 dimensional) of 24 elements using the dim() function.

#Let's start with creating a vector w/ 24 elements.

vec <- sample(1:5,24,replace = TRUE)

# using dim, we are making it a 3 dimensional array

dim(vec) <- c(3,2,4)

dim(vec) #Now we have a 3 dimensional array with 24 elements.
## [1] 3 2 4

4.2.2 Create an array (3 dimensional) of 24 elements using the array() function.

ar <- array(1:5,c(2,3,4)) #the second statement represents the dimensions.

4.2.3 Assign some dimnames of your choice to the array using the dimnames() function.

ar <- array(data = 1:24,
            dim  = c(2, 3, 4))
            
# easiest way is giving the names seperately i guess

dimnames(ar)[[1]] <- c("a","b")

dimnames(ar)[[2]] <- c("x","y","z")

dimnames(ar)[[3]] <- c("o","n","u","r")

ar
## , , o
## 
##   x y z
## a 1 3 5
## b 2 4 6
## 
## , , n
## 
##   x  y  z
## a 7  9 11
## b 8 10 12
## 
## , , u
## 
##    x  y  z
## a 13 15 17
## b 14 16 18
## 
## , , r
## 
##    x  y  z
## a 19 21 23
## b 20 22 24

4.2.4 Assign some dimnames of your choice to the array using the arguments of the array() function.

ar <- array(data = 1:24,
            dim  = c(2, 3, 4),
            dimnames = list(c("a","b"),c("x","y","z"),c("o","n","u","r"))) #Instead of dimnames function, you can use it as an argument inside array function.

ar
## , , o
## 
##   x y z
## a 1 3 5
## b 2 4 6
## 
## , , n
## 
##   x  y  z
## a 7  9 11
## b 8 10 12
## 
## , , u
## 
##    x  y  z
## a 13 15 17
## b 14 16 18
## 
## , , r
## 
##    x  y  z
## a 19 21 23
## b 20 22 24

4.2.5 Instead of column-major array, make a row-major array (transpose).

#This is how i do it. Please let me know if you know any other way.

aperm(ar, perm = NULL, resize = TRUE)
## , , a
## 
##    x  y  z
## o  1  3  5
## n  7  9 11
## u 13 15 17
## r 19 21 23
## 
## , , b
## 
##    x  y  z
## o  2  4  6
## n  8 10 12
## u 14 16 18
## r 20 22 24

4.2.6 For this exercise, and all that follow, download thisfile, and read it into R using the read.csv() function.
Copy the column named N into a new variable arr.

setwd("/Users/Onurhan/Documents/R Work/R-exercises Files/")   # set working directory

dataset <- read.csv("exercise426.csv") #as you can see, N column is the only column in the dataset.

arr <- dataset$N

4.2.7 Set dimensions of this variable and convert it into a 3 * 2 * 4 array. Add dimnames.

arr <- array(arr,
             dim = c(3,2,4),
             dimnames = list(c("a","b","c"),c("x","y"),c("o","n","u","r")))

arr
## , , o
## 
##   x y
## a 1 4
## b 2 5
## c 3 6
## 
## , , n
## 
##   x  y
## a 7 10
## b 8 11
## c 9 12
## 
## , , u
## 
##    x  y
## a 13 16
## b 14 17
## c 15 18
## 
## , , r
## 
##    x  y
## a 19 22
## b 20 23
## c 21 24

4.2.8 Print the whole array on the screen.

print(arr)
## , , o
## 
##   x y
## a 1 4
## b 2 5
## c 3 6
## 
## , , n
## 
##   x  y
## a 7 10
## b 8 11
## c 9 12
## 
## , , u
## 
##    x  y
## a 13 16
## b 14 17
## c 15 18
## 
## , , r
## 
##    x  y
## a 19 22
## b 20 23
## c 21 24

4.2.9 Print only elements of height 2, assuming the first dimension represents rows, the second columns and the third height.

arr[,,2]
##   x  y
## a 7 10
## b 8 11
## c 9 12

4.2.10 Print elements of height 1 and columns 3 and 1.

arr[,-2,1] #There are only 2 columns for each height? So Only the first column of first height will be printed.
## a b c 
## 1 2 3

4.2.11 Print element of height 2, column 4 and row 2.

# arr[2,4,2]. There is no such element because there are only 2 columns.

4.2.12 Repeat the exercises 9-11, but instead of using numbers to reference row, column and height, use dimnames.

arr[,,"n"]
##   x  y
## a 7 10
## b 8 11
## c 9 12
arr[,"x","o"] # The question was 1 & 3 however there is no 3rd column. If there was a 3rd column, argument would include that one too with list("x","3rd one")
## a b c 
## 1 2 3
arr["b",,"n"]
##  x  y 
##  8 11