0% found this document useful (0 votes)
2 views

Arrays

The document explains the concept of arrays in R programming, highlighting their similarities to matrices and their ability to handle multiple dimensions. It details how to create arrays, access their elements, and perform operations such as addition and subtraction, along with functions for checking dimensions and names. Additionally, it introduces the apply() function for applying operations across array elements.

Uploaded by

19A540 MAHESH
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Arrays

The document explains the concept of arrays in R programming, highlighting their similarities to matrices and their ability to handle multiple dimensions. It details how to create arrays, access their elements, and perform operations such as addition and subtraction, along with functions for checking dimensions and names. Additionally, it introduces the apply() function for applying operations across array elements.

Uploaded by

19A540 MAHESH
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

ARRAYS

Arrays are similar to matrices but can have more than two dimensions.

myarray <- array(vector, dimensions, dimnames)

Where vector contains the data for the array, dimensions is a numeric vector giving the
maximal index for each dimension, and dimnames is an optional list of dimension labels
Three-dimensional (2x3x4) array of numbers

As you can see, arrays are a natural extension of matrices. They can be
useful in programming new statistical methods. Like matrices, they must
be a single mode.

Identifying elements follows what you’ve seen for matrices. In the


previous example, the z[1,2,3] element is 15.
Accessing elements of an array

In R programming, we can use the index position to


access the array elements. Using the index, we can
access or alter/change each and every individual
element present in it. Index value starts at 1 and
ends at n, where n is the size of a matrix, row, or
column

Array_Name[row_position, Column_Position,
Matrix_Level].
Functions -Array
is.array()
Check for array if the object is an array or not. The function
name is.array() is a primitive function that lets you do so. Its
gives output in terms True or False

vector1 <- c(2,18,30)


vector2 <- c(10,14,17,13,11,15,22,11,33)
column.names <- c("COL1","COL2","COL3","COL4")
row.names <- c("ROW1","ROW2","ROW3")
matrix.names <- c("Matrix1","Matrix2")
result <- array(c(vector1,vector2),dim = c(3,4,2),dimnames =
list(row.names,column.names,
matrix.names))
result[,,1] result[,,2] is.array(result)
Check size of Array

Knowing dimensionality, a number of rows, columns of array


helps in slicing and dicing of data. Here are some functions to
do that: dim, nrow, ncol

vector1 <- c(2,18,30)


vector2 <- c(10,14,17,13,11,15,22,11,33)
result <- array(c(vector1,vector2),dim = c(3,4,2))
print(result)
dim(result)
nrow(result)
ncol(result)
Check names of row and columns

In order to know the names of rows, columns and dimension


names of an array. Below are the shown implementation of it.

vector1 <- c(2,18,30)


vector2 <- c(10,14,17,13,11,15,22,11,33)
column.names <- c("COL1","COL2","COL3","COL4")
row.names <- c("ROW1","ROW2","ROW3")
matrix.names <- c("Matrix1","Matrix2")
result <- array(c(vector1,vector2),dim = c(3,4,2),dimnames =
list(row.names,column.names,
matrix.names))
rownames(result)
colnames(result)
dimnames(result)
Calculations on Array element

A function name apply(), helps in applying any operation across


array elements.

apply(x, margin, fun)

Here x is an array, the margin here refers to either rows or


columns.

MARGIN=1 for row-wise operation


MARGIN=2 for column-wise operation
MARGIN=c(1,2) for both.

Fun is the function applied across elements in the array of the


data frame. These could be the standard functions that are part
of R or custom functions (user-defined)
Row Wise R Code:

vector1 <- c(2,18,30)


vector2 <- c(10,14,17,13,11,15,22,11,33)
column.names <- c("COL1","COL2","COL3","COL4")
row.names <- c("ROW1","ROW2","ROW3")
matrix.names <- c("Matrix1","Matrix2")
result <- array(c(vector1,vector2),dim = c(3,4,2),dimnames =
list(row.names,column.names,
matrix.names))
result[,,1] result[,,2] apply(result[,,1],1,sum)
Column Wise – R Code:

vector1 <- c(2,18,30)


vector2 <- c(10,14,17,13,11,15,22,11,33)
column.names <- c("COL1","COL2","COL3","COL4")
row.names <- c("ROW1","ROW2","ROW3")
matrix.names <- c("Matrix1","Matrix2")
result <- array(c(vector1,vector2),dim = c(3,4,2),dimnames =
list(row.names,column.names,
matrix.names))
result[,,1] result[,,2] apply(result[,,1],2,sum)

The function applied is also base for other complex functions like lapply(), rapply(), etc.
Addition & Subtraction

The multidimensional matrix has to be converted to the one-


dimensional matrix, in order to be added or subtracted.

•Addition:
vector1 <- c(2,18,30)
vector2 <- c(10,14,17,13,11,15,22,11,33)
column.names <-
c("COL1","COL2","COL3","COL4")
row.names <- c("ROW1","ROW2","ROW3")
matrix.names <- c("Matrix1","Matrix2")
result <- array(c(vector1,vector2),dim =
c(3,4,2),dimnames =
list(row.names,column.names,
matrix.names))
result[,,1] result[,,2] print(result[,,1] +
result[,,2])
•Subtraction:

vector1 <- c(2,18,30)


vector2 <- c(10,14,17,13,11,15,22,11,33)
column.names <- c("COL1","COL2","COL3","COL4")
row.names <- c("ROW1","ROW2","ROW3")
matrix.names <- c("Matrix1","Matrix2")
result <- array(c(vector1,vector2),dim = c(3,4,2),dimnames =
list(row.names,column.names,
matrix.names))
result[,,1] result[,,2] print(result[,,1] - result[,,2])

You might also like