# Solved: Create a array with values from 21 to 68 with dimension 2,4,6? Print the element of row 2, column 4, and height 2

#### ByRaju Chaudhari

Oct 20, 2020

Create a array with values from 21 to 68 with dimension 2,4,6? Print the element of row 2, column 4, and height 2? Change the names of the Rows to R1, R2, etc.? Change the names of the columns to C!, C2, etc.? Change the names of the height to H1, H2, etc.?

#### Solution

First create an array using array() function with values from 21 to 68 with dimension 2,4,6.

## Create an array of specified dimension
my_array<-array(21:68,dim = c(2,4,6))
## Print the complete array
my_array
, , 1

[,1] [,2] [,3] [,4]
[1,]   21   23   25   27
[2,]   22   24   26   28

, , 2

[,1] [,2] [,3] [,4]
[1,]   29   31   33   35
[2,]   30   32   34   36

, , 3

[,1] [,2] [,3] [,4]
[1,]   37   39   41   43
[2,]   38   40   42   44

, , 4

[,1] [,2] [,3] [,4]
[1,]   45   47   49   51
[2,]   46   48   50   52

, , 5

[,1] [,2] [,3] [,4]
[1,]   53   55   57   59
[2,]   54   56   58   60

, , 6

[,1] [,2] [,3] [,4]
[1,]   61   63   65   67
[2,]   62   64   66   68

To print the element of row 2, column 4, and height 2 use the indexing.

## Print the element of row 2, column 4 and height 2
my_array[2,4,2]
[1] 36

Create a character vector for row as Row, for column as Col and for height as Hig. Remember that the length of the each character vector match with the length of dimension (i.e. 2 for row, 4 for column and 6 for height).

Row<-c("R1","R2")
Col<-c("C1","C2","C3","C4")
Hig<-c("H1","H2","H3","H4","H5","H6")

Use dimnames() function to assign dimension names to array as follows:

dimnames(my_array)<-list(Row,Col,Hig)
## Display the dimension names
dimnames(my_array)
[[1]]
[1] "R1" "R2"

[[2]]
[1] "C1" "C2" "C3" "C4"

[[3]]
[1] "H1" "H2" "H3" "H4" "H5" "H6"
## Display complete array with dimension names
my_array
, , H1

C1 C2 C3 C4
R1 21 23 25 27
R2 22 24 26 28

, , H2

C1 C2 C3 C4
R1 29 31 33 35
R2 30 32 34 36

, , H3

C1 C2 C3 C4
R1 37 39 41 43
R2 38 40 42 44

, , H4

C1 C2 C3 C4
R1 45 47 49 51
R2 46 48 50 52

, , H5

C1 C2 C3 C4
R1 53 55 57 59
R2 54 56 58 60

, , H6

C1 C2 C3 C4
R1 61 63 65 67
R2 62 64 66 68