# R Arrays

Used to store multiple elements of the same data type under a single identifier. They can be accessed using an index that starts from 0

### Example

``````# Creating a 3D array
arr3d <- array(1:24, dim = c(2, 3, 4))
# A 3D array with dimensions 2 x 3 x 4``````

## Accessing Elements

We can access the arrays with index number inside a [] bracket.

### Example

``````# Accessing elements
arr3d <- array(1:24, dim = c(2, 3, 4))
element <- arr3d[1, 2, 3]
element``````

## Array Operators

Operators are used in arrays to perform various operations.

Operation

Syntax

Description

Example

arr1 + arr2

array(1:24, dim = c(2, 3, 4)) + array(24:48, dim = c(2, 3, 4))

Subtraction

arr1 - arr2

Element-wise subtraction of one vector from another.

array(1:24, dim = c(2, 3, 4)) - array(24:48, dim = c(2, 3, 4))

Multiplication

arr * scalar or arr1 * arr2

Scalar multiplication or element-wise multiplication.

array(1:24, dim = c(2, 3, 4)) * 2

Division

arr / scalar or arr1 / arr2

Scalar division or element-wise division.

array(1:24, dim = c(2, 3, 4)) / 2

Exponentiation

arr^exp

Element-wise exponentiation.

array(1:24, dim = c(2, 3, 4))^2

### Example

``````array(1:24, dim = c(2, 3, 4)) + array(24:48, dim = c(2, 3, 4))
array(1:24, dim = c(2, 3, 4)) - array(24:48, dim = c(2, 3, 4))
array(1:24, dim = c(2, 3, 4))*2
array(1:24, dim = c(2, 3, 4))/2
array(1:24, dim = c(2, 3, 4))^2``````

## Array Functions

Functions are a built in methods that performs operations fast and easy.

Operation

Syntax

Description

Example

Dimensions

dim(arr)

Returns the dimensions of an array.

dimensions <- dim(arr3d)

Sum

sum(arr)

Calculates the sum of all elements in an array.

arr_sum_all <- sum(arr3d)

Mean

mean(arr)

Calculates the mean (average) of elements in an array.

arr_mean <- mean(arr3d)

Length

length(arr)

Applies a function to the specified margins of an array.

arr_length <- length(arr3d)

Applying a Function

apply(arr, margin, fun)

Applies a function to the specified margins of an array.

row_sums <- apply(arr3d, 1:2, sum)

Array Flattening

arr^exp

Flattens an array into a one-dimensional vector.

flattened_vector <- as.vector(arr3d)

### Example

``````arr3d <- array(1:24, dim = c(2, 3, 4))
dim(arr3d)
sum(arr3d)
mean(arr3d)
length(arr3d)
apply(arr3d, 1:2, sum)
as.vector(arr3d)``````

# Quick Recap - Topics Covered

R Arrays

## Practice With Examples in Compilers

The Concepts and codes you leart practice in Compilers till you are confident of doing on your own. A Various methods of examples, concepts, codes availble in our websites. Don't know where to start Down some code examples are given for this page topic use the code and compiler.

### Example js

Example 1 Example 2 Example 3 Example 4 Example 5

FEEDBACK