R Unit2
R Unit2
, , 1 , , 2
[,1] [,2] [,1] [,2]
[1,] 5 7 [1,] 1 3
[2,] 2 4 [2,] 2 4
DIMENSION NAMES
We can give names to the rows, columns and matrices in the
array by using the dimnames parameter.
ROW1 ROW2
ROW3 3 12 15
[1] 13
ROW1 ROW2
ROW3 COL1 5
10 13
COL2 9 11 14
COL3 3 12 15
Dimensions of array in R-
In R, the dimensions of an array define its structure and determine how the data
is organized across multiple dimensions. You can specify the dimensions of an
array using the dim argument in the array() function or by assigning values to
the dim() attribute of an existing array. Let's explore how dimensions work in R
arrays:
1. Specifying Dimensions with array() Function:
You can create an array in R and specify its dimensions using the dim argument
in the array() function.
# Create a 3x3x2 array filled with random numbers
arr <- array(data = runif(18), dim = c(3, 3, 2))
# Display the array
arr
In this example, c(3, 3, 2) specifies the dimensions of the array, indicating that
it has 3 rows, 3 columns, and 2 layers.
2. Assigning Dimensions to an Existing Array:
You can also assign dimensions to an existing array using the dim() function.
# Create a matrix
mat <- matrix(1:9, nrow = 3, ncol = 3)
# Convert the matrix to an array and assign dimensions
arr_from_mat <- array(mat, dim = c(3, 3, 1))
# Display the array
arr_from_mat
In this example, c(3, 3, 1) assigns dimensions to the array, specifying 3 rows, 3
columns, and 1 layer.
3. Accessing Dimensions:
You can access the dimensions of an array using the dim() function.
# Get the dimensions of the array
array_dim <- dim(arr)
# Display the dimensions
array_dim
Conclusion:
Dimensions define the structure of an array in R, specifying the number of rows,
columns, and layers (or additional dimensions) it contains. You can specify
dimensions when creating an array or assign them to an existing array.
Understanding and managing dimensions are essential for working with multi-
dimensional data in
Indexing Arrays:
In R, arrays are multi-dimensional objects, and indexing allows you to access
specific elements or subsets of an array.
Basic Indexing:
You can access individual elements of an array by specifying indices for each
dimension within square brackets [ ].
EX-
# Create a 3x3x2 array
arr <- array(1:18, dim = c(3, 3, 2))
# Accessing elements
arr[2, 3, 1] # Access element in the second row, third column, and first layer
Slicing:
You can extract subsets of an array using slicing, specifying ranges of indices for
each dimension.
EX-
# Slicing to extract a subset of the array
subset_arr <- arr[1:2, , ] # Extract first two rows for all columns and layers
Logical Indexing:
You can use logical vectors to subset elements of an array based on specific
conditions.
EX-
# Logical indexing to filter elements
filtered_arr <- arr[arr > 10] # Extract elements greater than 10
mension names
Functions for arrays in R-
1. Creating Arrays:
array(): Create an array from data and specified dimensions.
EX-
arr <- array(data = 1:12, dim = c(3, 2, 2))
2. Summarizing Arrays:
sum(): Calculate the sum of elements in an array.
EX-
total_sum <- sum(arr)
mean(): Compute the mean of elements in an array.
EX-
avg_value <- mean(arr)
min() and max(): Find the minimum and maximum values in an array.
3. Aggregating Functions:
Calculations across Array Elements
We can do calculations across the elements in an array using the
apply()function.
Syntax
apply(x, margin, fun)
Following is the description of the parameters used:
x is an array.
margin is the name of the data set used.
fun is the function to be applied across the elements of the array.
Example
We use the apply() function below to calculate the sum of the
elements in the rows of an array across all the matrices.
print(new.array)
# Use apply to calculate the sum of the rows across all
the matrices.
result <- apply(new.array, c(1), sum)
print(result)
[1] 56 68 60
EX-
row_sums <- apply(arr, 1, sum) # Calculate row-wise sum
rowSums() and colSums(): Compute row-wise or column-wise sums.
4. Statistical Functions:
quantile(): Compute quantiles of elements in an array.
sd(): Calculate the standard deviation of elements in an array.
var(): Compute the variance of elements in an array.
5. Conditional Functions:
which(): Return the indices of array elements satisfying a condition.
ifelse(): Perform conditional operations on array elements.
6.Mathematical functions-
log(), exp(), sqrt(): Apply logarithmic, exponential, and square
root functions to array elements.
sin(), cos(), tan(): Apply trigonometric functions.