EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials R Programming Tutorial Arrays in R
Secondary Sidebar
R programming Tutorial
  • Data Structure
    • R list
    • Arrays in R
    • Data Frames in R
    • Factors in R
    • R Vectors
  • Basic
    • What is R Programming Language
    • Careers in R Programming
    • Install R
    • List of R Packages
    • Introduction of R Tools Technology
    • R Programming Language
    • DataSet in R
    • What is RStudio?
    • R-studio-Functions
    • R Packages
    • Time series?in R
    • R Data Types
    • R for data science
    • R Operators
    • R Data Frame
    • R Analytics Tool
    • R Tree Package
    • Vectors in R
  • Control statement
    • If Statement in R
    • If Else Statement in R
    • Else if in R
    • Switch Statement in R
  • Loops
    • Loops in R
    • For Loop in R
    • Nested For Loop in R
    • While Loop in R
    • Next in R
  • Chart/graphs
    • Graphs in R
    • Bar Charts in R
    • Pie Chart in R
    • Histogram in R
    • Line Graph in R
    • Plot Function in R
    • Scatterplot in R
    • R Boxplot labels
  • Regression in R
    • Simple Linear Regression in R
    • Linear Regression in R
    • Multiple Linear Regression in R
    • Logistic Regression in R
    • Poisson Regression in R
    • OLS Regression in R
    • P-Value in Regression
  • Anova in R
    • ANOVA in R
    • One Way ANOVA in R
    • Two Way ANOVA in R
  • Advanced
    • Statistical Analysis with R
    • R String Functions
    • Data Exploration in R
    • R CSV Files
    • KNN Algorithm in R
    • Sorting in R
    • lm Function in R
    • Hierarchical Clustering in R
    • R Normal Distribution
    • Binomial Distribution in R
    • Decision Tree in R
    • GLM in R
    • Arima Model in R
    • Linear Model in R
    • Predict Function in R
    • Survival Analysis in R
    • Standard Deviation in R
    • Statistical Analysis in R
    • Predictive Analysis?in R
    • T-test in R
    • Database in R
  • Programs
    • Functions in R
    • Boxplot in R
    • R Program Functions
    • Factorial in R
    • Random Number Generator in R
  • Interview question
    • R Interview Questions

Related Courses

R Programming Certification Course

Statistical Analysis Course Training

All in One Data Science Courses

Arrays in R

By Priya PedamkarPriya Pedamkar

Arrays in R

Introduction to Arrays in R

Arrays in R is a feature that stores the similar types of data for processing. It accepts any number of elements to store. Arrays can be of one or multiple dimensions. The arrays created using vectors of one dimension is known as a one-dimensional array. Similarly Matrices used to store data in the R language are considered as a two-dimensional array. Array provides ease of access to the stored data element using the array index. Arrays in R languages are a preferred programming component while developing data processing for multiple data elements. R language supports Array function to construct the array and assign the data values.

Arrays in R

Pictorial representation: Vector, Matrix, Array

  • One dimensional array referred to as a vector.
  • Two-dimensional array referred to as a matrix.

Syntax:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Here is the syntax of array:

Array_NAME <- array(data, dim = (row_Size, column_Size, matrices, dimnames)

  • data – Data is an input vector that is fed to the array.
  • matrices – This refers to the dimensionality of matrices. Array in R can be multi-dimensional matrices.
  • row_Size – row_Size depicts the number of rows that an array will comprise of.
  • column_Size – column_Size depicts the number of columns that an array will comprise of.
  • dimnames – This field if for changing the default names of rows and columns to the user’s wish/preference.

Properties:

  • It’s homogeneous. That means it can store the same type of data.
  • It stores data in contiguous memory
  • Array elements can be accessed by knowing the index number.

How to Create an array in R?

Below are different scenarios on how to create an array in r as follows:

Scenario 1

Let’s create an array that would be 3×4 matrices. Here 3 will row and 4 will be columns, matrices will be one. As our initial steps, let’s keep dimnames = NULL(which is a default value, if nothing specified ).

This is a one-dimensional matrix

R Code:

vector1 <- c(2,18,30)
vector2 <- c(10,14,17,13,11,15,22,11,33)
print(array(c(vector1, vector2),dim = c(3,4,1)))

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,241 ratings)

Output:

R programing

In order to check if finally created array got created or not.

Once array gets created:

Result

<- array(c(vector1, vector2),dim = c(3,4,1)))

The function “class” can help you with that.

class(Result)

R Code:

vector1 <- c(2,18,30)
vector2 <- c(10,14,17,13,11,15,22,11,33)
print(array(c(vector1, vector2),dim = c(3,4,1)))
Result <- array(c(vector1, vector2),dim = c(3,4,1))
class(Result)

Output:

R programing 1

In order to check the dimension’s product of the array, one can use function: length.

R Code:

vector1 <- c(2,18,30)
vector2 <- c(10,14,17,13,11,15,22,11,33)
print(array(c(vector1, vector2),dim = c(3,4,1)))
Result <- array(c(vector1, vector2),dim = c(3,4,1))
length(Result)

Output:

R programing 2

Scenario 2

Let’s create the same array which would be 3×4 matrices. Here again, 3 will be a row and 4 will be columns, but matrices will be two. Let’s keep dimnames = NULL(which is a default value, if nothing specified ).

R code:

vector1 <- c(2,18,30)
vector2 <- c(10,14,17,13,11,15,22,11,33)
print(array(c(vector1,vector2),dim = c(3,4,2)))

Output:

R programing 3

Scenario 3

Let’s create the same array which would be 3×4 matrices. Here again, 3 will be row and 4 will be columns, but matrices will be two. Let’s see for values assigned to dimnames.

R code:

vector1 <- c(2,18,30)
vector2 <- c(10,14,17,13,11,15,22,11,33)
column.names <- c("COL1","COL2","COL3","COL4")
row.names <- c("ROW1","ROW2","ROW3")
matrix.names <- c("Matrix1","Matrix2")
array(c(vector1,vector2),dim = c(3,4,2),dimnames = list(row.names,column.names,
matrix.names ))

Output:

R programming 4

Now we are good at creating an array of any dimensionality. Let’s now focus on the way of accessing any element in an array.

How to Create an Access Elements array in R?

Below are different access elements on how to create an array in r as follows –

Scenario 1

Let’s say we have the same array in R:

R Code:

vector1 <- c(2,18,30)
vector2 <- c(10,14,17,13,11,15,22,11,33)
column.names <- c("COL1","COL2","COL3","COL4")
row.names <- c("ROW1","ROW2","ROW3")
matrix.names <- c("Matrix1","Matrix2")
result <- array(c(vector1,vector2),dim = c(3,4,2),dimnames = list(row.names,column.names,
matrix.names))

Output:

R programming 5

Now, we need to access 3rd row,3rd column of the second matric in the array.

R Code:

vector1 <- c(2,18,30)
vector2 <- c(10,14,17,13,11,15,22,11,33)
column.names <- c("COL1","COL2","COL3","COL4")
row.names <- c("ROW1","ROW2","ROW3")
matrix.names <- c("Matrix1","Matrix2")
result <- array(c(vector1,vector2),dim = c(3,4,2),dimnames = list(row.names,column.names,
matrix.names))
result[3,3,2]

Output:

R programming 6

To summarize this, square brackets are used to denote an index. To specify the index in arrays, there are four choices available: positive integers, negative integers, logical values, element names

Scenario 2

One needs to access an entire 1st array matrix:

R Code:

vector1 <- c(2,18,30)
vector2 <- c(10,14,17,13,11,15,22,11,33)
column.names <- c("COL1","COL2","COL3","COL4")
row.names <- c("ROW1","ROW2","ROW3")
matrix.names <- c("Matrix1","Matrix2")
result <- array(c(vector1,vector2),dim = c(3,4,2),dimnames = list(row.names,column.names,
matrix.names))
result[,,1]

Output:

array Example

Different Array Operation With Examples

This section will provide you grip over various operations carried out on arrays for achieving various results.

1. Addition & Subtraction

The multidimensional matrix has to be converted to the one-dimensional matrix, in order to be added or subtracted.

  • Addition:

R code:

vector1 <- c(2,18,30)
vector2 <- c(10,14,17,13,11,15,22,11,33)
column.names <- c("COL1","COL2","COL3","COL4")
row.names <- c("ROW1","ROW2","ROW3")
matrix.names <- c("Matrix1","Matrix2")
result <- array(c(vector1,vector2),dim = c(3,4,2),dimnames = list(row.names,column.names,
matrix.names))
result[,,1] result[,,2] print(result[,,1] + result[,,2])

Output:

Addition

  • Subtraction:

R code:

vector1 <- c(2,18,30)
vector2 <- c(10,14,17,13,11,15,22,11,33)
column.names <- c("COL1","COL2","COL3","COL4")
row.names <- c("ROW1","ROW2","ROW3")
matrix.names <- c("Matrix1","Matrix2")
result <- array(c(vector1,vector2),dim = c(3,4,2),dimnames = list(row.names,column.names,
matrix.names))
result[,,1] result[,,2] print(result[,,1] - result[,,2])

Output:

Output

2. Calculations on Array element

A function name apply(), helps in applying any operation across array elements.

Syntax:

apply(x, margin, fun)

Here x is an array, the margin here refers to either rows or columns.

  • MARGIN=1 for row-wise operation
  • MARGIN=2 for column-wise operation
  • MARGIN=c(1,2) for both.

Fun is the function applied across elements in the array of the data frame. These could be the standard functions that are part of R or custom functions (user-defined)

Example 1:

Row Wise R Code:

vector1 <- c(2,18,30)
vector2 <- c(10,14,17,13,11,15,22,11,33)
column.names <- c("COL1","COL2","COL3","COL4")
row.names <- c("ROW1","ROW2","ROW3")
matrix.names <- c("Matrix1","Matrix2")
result <- array(c(vector1,vector2),dim = c(3,4,2),dimnames = list(row.names,column.names,
matrix.names))
result[,,1] result[,,2] apply(result[,,1],1,sum)

Output:

Function

Column Wise – R Code:

vector1 <- c(2,18,30)
vector2 <- c(10,14,17,13,11,15,22,11,33)
column.names <- c("COL1","COL2","COL3","COL4")
row.names <- c("ROW1","ROW2","ROW3")
matrix.names <- c("Matrix1","Matrix2")
result <- array(c(vector1,vector2),dim = c(3,4,2),dimnames = list(row.names,column.names,
matrix.names))
result[,,1] result[,,2] apply(result[,,1],2,sum)

Output:

Function 2

This gives output in vector form which contains sum of individual columns. Here “sum” is the standard R function.

Example 2

R Code:

vector1 <- c(2,18,30)
vector2 <- c(10,14,17,13,11,15,22,11,33)
column.names <- c("COL1","COL2","COL3","COL4")
row.names <- c("ROW1","ROW2","ROW3")
matrix.names <- c("Matrix1","Matrix2")
result <- array(c(vector1,vector2),dim = c(3,4,2),dimnames = list(row.names,column.names,
matrix.names))
result[,,1] result[,,2] apply(result[,,1],1,function(x)  x+10)

Output:

Row

This gives the output of the same dimension. The thing to notice here is, we have applied a user-defined function. This function is very useful and powerful while solving real-world problems. The function applied is also base for other complex functions like lapply(), rapply(), etc.

3. Check for Array

Check for array if the object is an array or not. The function name is.array() is a primitive function that lets you do so. Its gives output in terms True or False

R Code:

vector1 <- c(2,18,30)
vector2 <- c(10,14,17,13,11,15,22,11,33)
column.names <- c("COL1","COL2","COL3","COL4")
row.names <- c("ROW1","ROW2","ROW3")
matrix.names <- c("Matrix1","Matrix2")
result <- array(c(vector1,vector2),dim = c(3,4,2),dimnames = list(row.names,column.names,
matrix.names))
result[,,1] result[,,2] is.array(result)

Output:

Check Array

4. Check size of Array

Knowing dimensionality, a number of rows, columns of array helps in slicing and dicing of data. Here are some functions to do that: dim, nrow, ncol

R code:

vector1 <- c(2,18,30)
vector2 <- c(10,14,17,13,11,15,22,11,33)
result <- array(c(vector1,vector2),dim = c(3,4,2))
print(result)
dim(result)
nrow(result)
ncol(result)

Output:

Check Size

5. Check names of row and columns

In order to know the names of rows, columns and dimension names of an array. Below are the shown implementation of it.

R code:

vector1 <- c(2,18,30)
vector2 <- c(10,14,17,13,11,15,22,11,33)
column.names <- c("COL1","COL2","COL3","COL4")
row.names <- c("ROW1","ROW2","ROW3")
matrix.names <- c("Matrix1","Matrix2")
result <- array(c(vector1,vector2),dim = c(3,4,2),dimnames = list(row.names,column.names,
matrix.names))
rownames(result)
colnames(result)
dimnames(result)

Output:

Row and columns

Conclusion

Going through the above content would have given you a clear understanding of arrays in R. R is a statistical language, and arrays are frequently used data objects. This means, working with varieties of operations like add, subtract, apply, etc. with an array in any application will now be a cakewalk for you.

Recommended Articles

This has been a guide to Array in R. Here we discuss an introduction to Arrays in R, properties of R, how to create an array in R, and some sample examples. You can also go through our other suggested articles to learn more –

  1. Install R
  2. VBA Arrays
  3. Data Types in MATLAB
  4. VBA ArrayList
Popular Course in this category
R Programming Training (13 Courses, 20+ Projects)
  13 Online Courses |  20 Hands-on Projects |  120+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Statistical Analysis Training (15 Courses, 10+ Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more