0% found this document useful (0 votes)
2 views1 page

2.4 Array

The document explains the concept of arrays in programming, highlighting their one-dimensional and two-dimensional structures compared to vectors and matrices. It provides an example of creating a three-dimensional array and demonstrates how to access and modify its elements. Additionally, it shows how to rename the dimensions of the array using the dimnames() function.

Uploaded by

2025508609
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

2.4 Array

The document explains the concept of arrays in programming, highlighting their one-dimensional and two-dimensional structures compared to vectors and matrices. It provides an example of creating a three-dimensional array and demonstrates how to access and modify its elements. Additionally, it shows how to rename the dimensions of the array using the dimnames() function.

Uploaded by

2025508609
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Array

Vectors is one-dimensional and matrices data structures is two-dimensional arrays. The array
function takes a vector or other objects and splits it into groups. If our data is multidimensional,
we need to use the array() function to create an array data structure. This book won’t go into
detail about arrays, but the examples provided are meant to show how arrays differ from vectors
and matrices.

xx<-array(1:24, c(3,4,2)) # value of 1 to 24 to form 3 rows, 4 columns, 2 layer


xx
,,1 #1st group
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12

,,2 #2nd group


[,1] [,2] [,3] [,4]
[1,] 13 16 19 22
[2,] 14 17 20 23
[3,] 15 18 21 24

class(xx)
[1] "array"

Access the aray using [row,column, layer]

xx[2,2,1] # extract value from row 2 column 2 group 1


xx[2,3,2]=555 # change value of row 2 column 3 group 2 to 555
xx

rename the row, column and group using dimnames()


xx<-array(1:24, c(3,4,2)) # create an array
dimnames(xx) <- list(
Row = c("Row1", "Row2", "Row3"), # rename row
Column = c("Col1", "Col2", "Col3", "Col4"), # rename column
Layer = c("group1", "group2") # rename layer
)
xx

You might also like