Table of contents
1.
Introduction
2.
Syntax of Arrays in R Programming 
3.
Arguments in Array
4.
Example:
4.1.
PHP
5.
Operations on Array in R Programming
5.1.
1. Naming Columns and Rows
5.2.
PHP
5.3.
2. Accessing Array elements
5.4.
PHP
5.5.
3. Manipulating Array Elements
5.6.
PHP
5.7.
4. Calculations Across Array Elements
5.8.
PHP
5.9.
5. Removal of Array elements
5.10.
PHP
5.11.
PHP
6.
Frequently Asked Questions
6.1.
What is R programming language?
6.2.
What do you mean by array data structure?
6.3.
How is an array created in R programming?
6.4.
What are the different data structures in R programming?
6.5.
What can different operations be performed on an array in R programming language?
7.
Conclusion
Last Updated: Oct 8, 2024
Easy

Arrays in R Programming

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Array in R Programming Language are the data objects which can store the data in more than two dimensions; let's say we create an array of dimensions (3, 3, 2), then it creates 2 rectangular matrices with 3 rows and 3 columns. Arrays can store the values having only a similar kind of data type.

An array is created using the array() function, which will take vectors as input and use the values in the dim parameter.

Arrays in R programming

Furthermore, we will go through its implementation, properties, and operations to understand this concept in a better way. 

As we know, Arrays in R programming are created using the array() function. So before going through the implementation part, let's see its syntax and the arguments it can take.

Syntax of Arrays in R Programming 

Array_NAME <- array(data, dim = (row_size, col_size, matrices, dimnames)


data – It is an input vector that is given to the array.

row_size – It describes the number of array elements an array can store.

col_size – It describes the number of column elements stored in an array.

matrices – Array in R programming consists of multi-dimensional matrices.

dimnames – It is used to change the default names given to rows and columns according to the user's choice.

Arguments in Array

The array() in R programming can be written as:

array(data = A, dim = length(data), dimname = NULL)


data- It is an input vector.

dim- It is an attribute that provides maximum indices in each dimension

dimname- It can be either NULL or have a name for the array.

Example:

In the example below, we will create an array of three 2x2 matrices having 2 rows and 2 columns.

  • PHP

PHP

# Two vectors of different lengths.
vec1 <- c(4, 5, 6, 7)
vec2 <- c(8, 9, 10, 11, 12)

# Take these vectors as mentioned above as input to the array.
res <- array(c(vec1,vec2),dim = c(2,2,3))
print(res)
You can also try this code with Online PHP Compiler
Run Code


Output:

, , 1
    [,1] [,2]
[1,]    4    5
[2,]    6    7
, , 2
    [,1] [,2]
[1,]    8    9
[2,]   10   11
, , 3
    [,1] [,2]
[1,]   12    4
[2,]    5    6


Explanation:

In the above-mentioned example, we have created two vectors of different lengths, and then three arrays are created of dimensions 2x2.

Operations on Array in R Programming

In this section, we will discuss different operations that can be performed on an array in R programming.

  1. Naming Columns and Rows
     
  2. Accessing Array elements
     
  3. Manipulating Array Elements
     
  4. Calculations Across Array Elements
     
  5. Removal of Array elements
     

Now let's learn about operations on array in R programming and their implementation in detail

1. Naming Columns and Rows

We can give names to the rows and columns using dimnames.

  • PHP

PHP

# Creating two Vectors of different lengths
vec1 <- c(4, 5, 6, 7)
vec2 <- c(8, 9, 10, 11, 12)

# Giving names to rows and columns
col.names <- c("C1", "C2", "C3")
row.names <- c("R1", "R2", "R3")
matrix.names <- c("Matrix1", "Matrix2")

res <- array(c(vec1, vec2), dim = c(3, 3, 2), dimnames = list(row.names, col.names, matrix.names))
print(res)
You can also try this code with Online PHP Compiler
Run Code


Output:

, , Matrix1
    C1 C2 C3
R1   4  5  6
R2   7  8  9
R3  10 11 12
, , Matrix2
    C1 C2 C3
R1   4  5  6
R2   7  8  9
R3  10 11 12


Explanation:

In the above code, we have given names to the rows and columns of our two matrices, each of dimension 3x3.

2. Accessing Array elements

We can access the array elements after giving vectors as input to the array.

  • PHP

PHP

# Creating two Vectors 
vec1 <- c(4, 5, 6, 7)
vec2 <- c(8, 9, 10, 11, 12)

# Giving names to rows and columns
col.names <- c("C1", "C2", "C3")
row.names <- c("R1", "R2", "R3")
matrix.names <- c("Matrix1", "Matrix2")

res <- array(c(vec1,vec2),dim = c(3,3,2),dimnames = list(row.names,
  col.names, matrix.names))

# Print the first matrix
print(res[,,1])

# Print the element in the 2nd row and 3rd column of the 2nd matrix.
print(res[2,3,2])
You can also try this code with Online PHP Compiler
Run Code


Output:

  C1 C2 C3
R1  4  5  6
R2  7  8  9
R3 10 11 12
# Output of res[2,3,2]
[1] 12

 

Explanation:

We have created two vectors of different lengths, and after creating two vectors, we will take these vectors as input to the array and print the first matrix and the element in the 2nd row and 3rd column of the second matrix.

3. Manipulating Array Elements

As we know array in R programming is made up of matrices in multiple dimensions so that the operations on elements have been carried out by accessing the elements.

  • PHP

PHP

# Creating two Vectors 
vec1 <- c(4, 5, 6, 7)
vec2 <- c(8, 9, 10, 11, 12)

# Giving names to rows and columns
col.names <- c("C1", "C2", "C3")
row.names <- c("R1", "R2", "R3")
matrix.names <- c("Matrix1", "Matrix2")

arr1 <- array(c(vec1,vec2),dim = c(3,3,2))
# Create another two vectors
vec3 <- c(1, 2, 3)
vec4 <- c(7, 8, 3, 9, 5, 0)
arr2 <- array(c(vec3,vec4),dim = c(3,3,2))

# Create matrices from arr1 and arr2.
matrix1 <- arr1[,,2]
matrix2 <- arr2[,,2]

# Add and print the result
res <- matrix1+matrix2
print(res)
You can also try this code with Online PHP Compiler
Run Code


Output:

    [,1] [,2] [,3]
[1,]   10   17   13
[2,]   16   14   17
[3,]   11   12   12

 

Explanation:

In the above-mentioned example, we are trying to explain the manipulation done between the elements of the two matrices created from the two different arrays; the two matrices have been added up and printed as a result.

4. Calculations Across Array Elements

We can perform calculations (like addition) across the elements in an array using the apply() function.

The syntax is as follows:

apply(arr, margin, func)


arr - Array

margin - Name of the data set

  • PHP

PHP

vec1 <- c(4, 5, 6)
vec2 <- c(7,10,11,12,13,14)

new.array <- array(c(vec1,vec2),dim = c(3,3,2))
print(new.array)

res <- apply(new.array, c(1), sum)
print(res)
You can also try this code with Online PHP Compiler
Run Code


Output:

, , 1
    [,1] [,2] [,3]
R1    4    7   10
R2    5   10   11
R3    6   12   12
, , 2
    [,1] [,2] [,3]
R1   13   13   14
R2   14   14   15
R3   15   15   16
[1] 22 24 27

Explanation:

We are performing addition on the array using apply() function.

5. Removal of Array elements

The elements of the array in R can be removed using %in% operator. Using this operator multiple elements can be removed at a time.

Syntax:

example[! example %in% c('x', 'y’, 'z')]


Let’s see an example of the array having character values

  • PHP

PHP

# Removal of array elements
x<- c('Coding','Ninjas', 'Coders', 'Programming')

# Let's remove Ninjas and Programming from the array
x <- x[! x %in% c('Ninjas', 'Programming')]

# Print the array
x
You can also try this code with Online PHP Compiler
Run Code


Output:

[1] "Coding" "Coders"


Explanation:

As you can see in the above example, both ‘Ninjas’ and ‘Programming’ are removed from the vector.

 

Let’s see an example of the vector having integer values.

  • PHP

PHP

# Removal of array elements
x<- c(1, 4, 6, 5, 7, 8, 3, 5, 9)

# Let's remove 1, 6, 5, 3 from the array
x <- x[! x %in% c(1, 6, 5, 3)]

# Print the array
x
You can also try this code with Online PHP Compiler
Run Code


Output

[1] 4 7 8 9


Explanation:                    

As you can see every occurrence of the values ‘1’, ‘6’, ‘5’ and ‘3’ has been removed from the vector as given in the code.

Check out this problem - Find Duplicate In Array

Frequently Asked Questions

What is R programming language?

R is an open-source programming language and environment for our purposes, statistical computing, and analysis or data science.

What do you mean by array data structure?

Arrays are the collection of similar data items stored at contiguous and adjacent memory locations.

How is an array created in R programming?

Array in R programming language is created using the array() function, which will take vectors as input and use the values in the dim parameter.

What are the different data structures in R programming?

Data Structures available in R programming are Vectors, Matrices, Lists, and Dataframe.

What can different operations be performed on an array in R programming language?

The operations that can be performed on the array in R programming are Naming the rows and columns, Accessing the elements, Manipulation of elements, and Calculations across array elements.

Conclusion

In this article, we’ve looked at arrays in R programming. We learned what arrays are, how to use them, their features, and the operations you can perform with them.

Keep reading to find more articles on similar topics and improve your R programming skills.

Recommended Articles

  1. R- Environment
  2. Introduction to Array
  3. Programming Languages
  4. Top Array Interview Questions 
  5. Interpolation in Angular
  6. Cognizant Eligibility Criteria

Recommended problems -

Also, check out some of the Guided Paths, Contests, Test SeriesInterview Bundles, and some Interview Experiences curated by top Industry Experts only on Code360

Live masterclass