R Language –Data Structures : Online Course
R Array
- In R Programming, arrays are like matrices but matrices are two-dimensional, consisting of fixed numbers of rows and columns.
- Arrays can store elements given on a sequence or vector in more than two dimensions.
- It consist of all elements of the same data type.
- The array() function is used to create an array in R.
- The
dim
parameter to specify the dimensions
R Array Syntax
Syntax for array() Function R
array( sequence/vector, dim=(no_of_rows, no_of_columns, no_of_matrix), dimnames=list(rownames, colnames, matrixnames) ) |
Parameters for array() Function in R
- Sequence / Vector: Here is the sequence or vector.
- Dim: Optional. DIM Parameter’s number of rows to be created on Value, the number of columns and the number of matrices is given.
- nrow : Number of rows
- ncol : Number of columns
- nmat : Number of matrices of dimensions nrow * ncol
- Dimnamese: Optional. Dimnames are named for Rows, Columns and Matrices on Parameter. Default value = NULL.
Array Creation
- In R, Arrays are the data objects which can store data in more than two dimensions.
- Creation of array is very simple.
- It is a data structure that represents a collection of same data types.
- An array is created using the array() function.
For Example: it creates 2 rectangular matrices each with 2 rows and 3 columns.
Input: create a array using array () function |
array(c(1, 2, 3, 4, 2, 5, 4, 4), dim = c(2,4,2)) |
Output: |
Name Row & Columns
In R, with the help of the dimname parameter of the array() function we can give the names to the rows, columns, and matrices of the array. By default, the rows, columns and matrices are named by their index values.
Input: Create two arrays and giving names to the rows, columns, and matrices. |
#Initializing names for rows, columns and matrices col_names <- c(“Col1″,”Col2″,”Col3”) row_names <- c(“Row1″,”Row2″,”Row3”) matrix_names <- c(“Matrix1″,”Matrix2”) #Creating two vectors of different lengths v1 <-c(10,11,12,13,14,15) v2 <-c(1,5,6) #Taking the vectors as input to the array res <- array(c(v1,v2),dim=c(3,3,2), dimnames=list(row_names,col_names,matrix_names)) print(res) |
Output: |
Accessing Array Elements
We can also access the elements of the array with the help of the index for different dimensions separated by commas. Let see an example to how we can access the elements of the array using the indexing method.
Input: Access the matrix present on nth row mth column, nth row, or mth column. |
#Initializing names for rows, columns and matrices col_names <- c(“Col1″,”Col2″,”Col3”) row_names <- c(“Row1″,”Row2″,”Row3”) matrix_names <- c(“Matrix1″,”Matrix2”) #Creating two vectors of different lengths arr = array(c(1,2,3,4, 2, 5, 4, 4), dim = c(2,4,2), dimnames=list(row_names,col_names,matrix_names)) arr #Print the element in the 2nd row and 4rd column of the 2nd matrix. arr[2, 4, 2] # Print the 2nd row of the second matrix of the array. arr[2, , 2] # Print the 2nd Matrix. arr[,,2] |
Output: |
Manipulating Array Elements
Since the array is composed of a matrix in multiple dimensions, operations on the elements of the array are performed by accessing the elements of the matrix.
Input: Example for Manipulating Array Elements |
arr1 = array(c(1,2,3,4, 2, 5, 4, 4), dim = c(2,4,2)) arr2 = array(c(6,7,3,8, 2, 5, 9, 4), dim = c(2,4,2)) #Creating matrices from these arrays mat1 <- arr1[,,2] mat2 <- arr2[,,2] res3 <- mat1+mat2 print(res3) |
Output: |
Calculations Across Array Elements
We will be using the apply()function for calculations in an array in R. The apply function performs a given function on all columns or all rows of an array or matrix.
Syntax
apply(x, margin, fun) |
- x is an array.
- a margin is the name of the dataset used.
- fun is the function to be applied to the elements of the array.
Input: Use apply to calculate the sum of the rows across all the matrices. |
arr1 = array(c(1,2,3,4, 2, 5, 4, 4), dim = c(2,4,2)) arr1 #Use apply to calculate the sum of the rows across all the matrices. col_sum <- apply(arr1,2,sum) col_sum |
Output: |
R Language: Online Course
आशा करता हूँ, कि यह आर्टिकल आपको पसंद आया होगा तो सोच क्या रहे हैं अभी इसी वक्त इसे अपने दोस्तों के साथ सोशल मीडिया पर Share करें।
Thanking You………………धन्यवाद………………..शुक्रिया………………..मेहरबानी…………………..
Read More
Reference