R Array Function and Create Array in R – An Ultimate Cheat Sheet

Placement-ready Online Courses: Your Passport to Excellence - Start Now

We will understand all the aspects related to the R array in this tutorial. We will cover different operations which are performed on rows and columns in an R array and an example to understand this concept in a better way.

Let’s start the tutorial.

Introduction to Array in R

In arrays, data is stored in the form of matrices, rows, and columns. We can use the matrix level, row index, and column index to access the matrix elements.

R arrays are the data objects which can store data in more than two dimensions. An array is created using the array() function. We can use vectors as input and create an array using the below-mentioned values in the dim parameter.

Get to know about all the R vector operations with example

R Array Syntax

Array_NAME <- array(data, dim = (row_Size, column_Size, matrices, dimnames)
  • data – Data is an input vector that is given to the array.
  • matrices – Array in R consists of multi-dimensional matrices.
  • row_Size – row_Size describes the number of row elements that an array can store.
  • column_Size – Number of column elements that can be stored in an array.
  • dimnames – Used to change the default names of rows and columns to the user’s preference.

Arguments in Array

The array function in R can be written as:

array(data = NA, dim = length(data), dimname = NULL)
  • data is a vector that provides data to fill the array.
  • dim attribute provides maximum indices in each dimension
  • dimname can be either NULL or can have a name for the array.

How to Create Array in R

Now, we will create an R array of two 3×3 matrices each with 3 rows and 3 columns.

# Create two vectors of different lengths.

vector1 <- c(2,9,3)
vector2 <- c(10,16,17,13,11,15)

# Take these vectors as input to the array.

result <- array(c(vector1,vector2),dim = c(3,3,2))
print(result)

When we execute the above code, it produces the following result:

Create Array in R code

Everything you need to know about R Matrix

Different Operations on Rows and Columns

1. Naming Columns And Rows

We can give names to the rows, columns, and matrices in the array by using the dimnames parameter.

# Create two vectors of different lengths.

vector1 <- c(2,9,6)
vector2 <- c(10,15,13,16,11,12)
column.names <- c("COL1","COL2","COL3")
row.names <- c("ROW1","ROW2","ROW3")
matrix.names <- c("Matrix1","Matrix2")

Code Display:

Naming Rows and Columns in R array

# Take these vectors as input to the array.

result <- array(c(vector1,vector2),dim = c(3,3,2),dimnames = list(row.names,column.names,
  matrix.names))
print(result)

When we execute the above code, it produces the following result:

Naming Rows and Columns in R array

Do you know about all the R Vector Functions

2. Accessing R Array Elements

# We will create two vectors of different lengths.

vector1 <- c(2,9,6)
vector2 <- c(10,15,13,16,11,12)
column.names <- c("COL1","COL2","COL3")
row.names <- c("ROW1","ROW2","ROW3")
matrix.names <- c("Matrix1","Matrix2")

Code Display:

Accessing R array elements

# Now, we will take these vectors as input to the array.

result <- array(c(vector1,vector2),dim = c(3,3,2), dimnames = list(row.names,column.names,
  matrix.names))
print(result)

When we execute the above code, it produces the following result:

Accessing R array elements

# Print the third row of the second matrix of the array.

print(result[3,,2])

# Print the element in the 1st row and 3rd column of the 1st matrix.

print(result[1,3,1])

# Print the 2nd Matrix.

print(result[,,2])

When we execute the above code, it produces the following result:

Accessing R array elements print code

Have you checked – Numeric and Character Functions in R

3. Manipulating R Array Elements

As the array is made up matrices in multiple dimensions, the operations on elements of an array are carried out by accessing elements of the matrices.

# Create two vectors of different lengths.

vector1 <- c(1,2,3)
vector2 <- c(3,4,5,6,7,8)

# Take these vectors as input to the array.

array1 <- array(c(vector1,vector2),dim = c(3,3,2))

# Create two vectors of different lengths.

vector3 <- c(3,2,1)
vector4 <- c(8,7,6,5,4,3)
array2 <- array(c(vector1,vector2),dim = c(3,3,2))

Code Display:

Manipulating R array elements

# create matrices from these arrays.

matrix1 <- array1[,,2]
matrix2 <- array2[,,2]

# Add the matrices.

result <- matrix1+matrix2
print(result)

When we execute the above code, it produces the following result:

Manipulating R array elements Output

Don’t forget to check the R Matrix Functions Tutorial

4. Calculations across R Array Elements

We will be using the apply()function for calculations in an array in R.

Syntax

apply(x, margin, fun)

Following is the description of the parameters used

  • 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.

For Example:

We use the apply() function below in different ways. To calculate the sum of the elements in the rows of an array across all the matrices.

# We will create two vectors of different lengths.

vector1 <- c(1,2,3)
vector2 <- c(3,4,5,6,7,8)

# Now, we will take these vectors as input to the array.

new.array <- array(c(vector1,vector2),dim = c(3,3,2))
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)

When we execute the above code, it produces the following result:

Calculation across R array elements Output

Extending Array Operations

In addition to the basic operations covered in this cheat sheet, R offers a multitude of functions and packages that enhance array manipulation capabilities. Functions like apply(), sapply(), lapply(), and tapply() provide versatile tools for applying functions across dimensions of arrays. Moreover, packages such as dplyr and tidyr offer a comprehensive set of functions for data manipulation, including array operations. By exploring these advanced tools and techniques, users can streamline their data analysis workflows and tackle complex array-based tasks with efficiency.

Advanced Array Operations and Performance Optimization

For large datasets or computationally intensive tasks, optimizing array operations becomes crucial. Vectorized operations in R are generally more efficient than iterative approaches, as they leverage optimized C code under the hood. Techniques such as parallel processing using packages like parallel or foreach can further enhance performance, distributing computations across multiple cores or nodes.

Moreover, exploring specialized array manipulation libraries like bigmemory or ff can be beneficial for handling massive datasets that exceed available memory capacity. By employing these advanced techniques, users can achieve significant improvements in processing speed and resource utilization when working with arrays in R.

Summary

We have studied arrays in R in a detailed manner with an example for a clear understanding of it. We can use the R array in daily life too. Also, we have learned different R array operations which will help you out with the concept of its applications.

Time to check your knowledge of R with the R Programming Online Quiz.

Did we exceed your expectations?
If Yes, share your valuable feedback on Google

courses

4 Responses

  1. Nidhi says:

    hi
    can you pls explain usage of apply function with arguments like what is does in thia example:
    result<-apply(new.array,c(1),sum)
    what is the function of c(1) here

    • Shivangi says:

      Hey,nidhi use of c(1)-
      MARGIN is a variable defining how the function is applied: when MARGIN=1 , it applies over rows, whereas with MARGIN=2 , it works over columns. Note that when you use the construct MARGIN=c(1,2) , it applies to both rows and columns; and. FUN , which is the function that you want to apply to the data

  2. Volker says:

    Hi,

    you describe under “1. Naming Columns And Rows” how to assign names to array dimensions.
    If I now have a list of array names like idx<-c("ROW1","COL1","Matrix1"), how do I address with this list the result array?
    result[idx] does not work…

    Thank you

  3. Elias says:

    your page is full of popup. I could not able to read your documents. How can i download the reading materials?

Leave a Reply

Your email address will not be published. Required fields are marked *