

{"id":4860,"date":"2017-12-05T09:56:33","date_gmt":"2017-12-05T09:56:33","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=4860"},"modified":"2024-05-08T21:34:44","modified_gmt":"2024-05-08T16:04:44","slug":"r-array","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/r-array\/","title":{"rendered":"R Array Function and Create Array in R &#8211; An Ultimate Cheat Sheet"},"content":{"rendered":"<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:1467,&quot;href&quot;:&quot;https:\\\/\\\/en.wikipedia.org\\\/wiki\\\/R_(programming_language)&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20251001042859\\\/https:\\\/\\\/en.wikipedia.org\\\/wiki\\\/R_(programming_language)&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2025-12-09 08:17:04&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-12 12:22:33&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-15 12:29:21&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-18 15:20:53&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-21 18:00:25&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-25 04:08:57&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-28 06:54:42&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-31 09:47:17&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-03 17:14:22&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-06 19:17:34&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-09 21:09:32&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-13 04:31:41&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-16 15:06:53&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-19 19:03:58&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-23 05:30:29&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-26 10:18:28&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-29 11:45:43&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-01 12:00:34&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-04 12:09:55&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-07 15:09:43&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-10 18:01:34&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-13 23:45:59&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-17 05:29:44&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-20 07:23:59&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-23 10:05:24&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-26 14:54:33&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-03-01 16:00:29&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-04 19:56:49&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-08 03:19:57&quot;,&quot;http_code&quot;:429},{&quot;date&quot;:&quot;2026-03-11 07:47:37&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-14 13:54:22&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-17 17:24:09&quot;,&quot;http_code&quot;:429},{&quot;date&quot;:&quot;2026-03-20 23:04:55&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-24 00:07:34&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-27 00:15:51&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-30 08:20:38&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-03 14:48:26&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-06 19:55:35&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-10 05:52:27&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-13 07:47:26&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-16 08:05:10&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-19 13:04:23&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-22 13:52:44&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-25 13:58:03&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-29 01:16:25&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-02 04:13:02&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-05 06:33:38&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-08 17:48:43&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-12 03:38:06&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-15 04:53:06&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-18 09:15:30&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-21 12:35:48&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-25 03:51:04&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-28 07:13:15&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-06-01 04:45:49&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-06-04 06:40:14&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-06-07 06:45:43&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-06-10 09:02:48&quot;,&quot;http_code&quot;:404}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-06-10 09:02:48&quot;,&quot;http_code&quot;:404},&quot;process&quot;:&quot;done&quot;}]'><\/div>\n<p>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.<\/p>\n<p>Let&#8217;s start the tutorial.<\/p>\n<h2>Introduction to Array in R<\/h2>\n<p>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.<\/p>\n<p>R arrays are the data objects which can store data in more than two dimensions. An array is created using the <strong>array() function<\/strong>.\u00a0We can use\u00a0vectors as input and create an array using the below-mentioned values in the dim parameter.<\/p>\n<p><em><strong>Get to know about all the<a href=\"https:\/\/data-flair.training\/blogs\/r-vector\/\"> R vector operations<\/a> with example<\/strong><\/em><\/p>\n<h3>R Array Syntax<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Array_NAME &lt;- array(data, dim = (row_Size, column_Size, matrices, dimnames)<\/pre>\n<ul>\n<li><strong>data &#8211;<\/strong> Data is an input vector that is given to the array.<\/li>\n<li><strong>matrices &#8211;<\/strong> Array in R consists of multi-dimensional matrices.<\/li>\n<li><strong>row_Size &#8211;<\/strong> row_Size describes the number of row elements that an array can store.<\/li>\n<li><strong>column_Size &#8211;<\/strong> Number of column elements that can be stored in an array.<\/li>\n<li><strong>dimnames &#8211;<\/strong>\u00a0Used to change the default names of rows and columns to the user\u2019s preference.<\/li>\n<\/ul>\n<h3>Arguments in Array<\/h3>\n<p>The array function in R can be written as:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">array(data = NA, dim = length(data), dimname = NULL)<\/pre>\n<ul>\n<li><strong>data<\/strong> is a vector that provides data to fill the array.<\/li>\n<li><strong>dim<\/strong> attribute provides maximum indices in each dimension<\/li>\n<li><strong>dimname<\/strong>\u00a0can be either NULL or can have a name for the array.<\/li>\n<\/ul>\n<h3>How to Create Array in R<\/h3>\n<p><span style=\"font-weight: 400\">Now, we will create an R array of two 3&#215;3 matrices each with 3 rows and 3 columns.<\/span><\/p>\n<p><strong># Create two vectors of different lengths.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">vector1 &lt;- c(2,9,3)\r\nvector2 &lt;- c(10,16,17,13,11,15)<\/pre>\n<p><strong># Take these vectors as input to the array.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">result &lt;- array(c(vector1,vector2),dim = c(3,3,2))\r\nprint(result)\r\n\r\n<\/pre>\n<p><strong>When we execute the above code, it produces the following result:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Create-Array-in-R-code.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-58688\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Create-Array-in-R-code.jpg\" alt=\"Create Array in R code\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Create-Array-in-R-code.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Create-Array-in-R-code-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Create-Array-in-R-code-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Create-Array-in-R-code-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Create-Array-in-R-code-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Create-Array-in-R-code-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p><em><strong>Everything you need to know about <a href=\"https:\/\/data-flair.training\/blogs\/r-matrix-operations-applications\/\">R Matrix<\/a><\/strong><\/em><\/p>\n<h2>Different Operations on Rows and Columns<\/h2>\n<h3>1. Naming Columns And Rows<\/h3>\n<p>We can give names to the rows, columns, and matrices in the array by using the dimnames parameter.<\/p>\n<p><strong># Create two vectors of different lengths.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">vector1 &lt;- c(2,9,6)\r\nvector2 &lt;- c(10,15,13,16,11,12)\r\ncolumn.names &lt;- c(\"COL1\",\"COL2\",\"COL3\")\r\nrow.names &lt;- c(\"ROW1\",\"ROW2\",\"ROW3\")\r\nmatrix.names &lt;- c(\"Matrix1\",\"Matrix2\")<\/pre>\n<p><strong>Code Display:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Naming-Rows-and-Columns-in-R-array.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-58689\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Naming-Rows-and-Columns-in-R-array.jpg\" alt=\"Naming Rows and Columns in R array\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Naming-Rows-and-Columns-in-R-array.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Naming-Rows-and-Columns-in-R-array-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Naming-Rows-and-Columns-in-R-array-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Naming-Rows-and-Columns-in-R-array-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Naming-Rows-and-Columns-in-R-array-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Naming-Rows-and-Columns-in-R-array-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p><strong># Take these vectors as input to the array.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">result &lt;- array(c(vector1,vector2),dim = c(3,3,2),dimnames = list(row.names,column.names,\r\n\u00a0\u00a0matrix.names))\r\nprint(result)<\/pre>\n<p><strong>When we execute the above code, it produces the following result:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Naming-Rows-and-Columns-in-R-array-2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-58690\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Naming-Rows-and-Columns-in-R-array-2.jpg\" alt=\"Naming Rows and Columns in R array\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Naming-Rows-and-Columns-in-R-array-2.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Naming-Rows-and-Columns-in-R-array-2-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Naming-Rows-and-Columns-in-R-array-2-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Naming-Rows-and-Columns-in-R-array-2-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Naming-Rows-and-Columns-in-R-array-2-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Naming-Rows-and-Columns-in-R-array-2-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p><em><strong>Do you know about all the <a href=\"https:\/\/data-flair.training\/blogs\/r-vector-functions\/\">R Vector Functions<\/a><\/strong><\/em><\/p>\n<h3>2. Accessing R Array Elements<\/h3>\n<p><strong># We will create two vectors of different lengths.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">vector1 &lt;- c(2,9,6)\r\nvector2 &lt;- c(10,15,13,16,11,12)\r\ncolumn.names &lt;- c(\"COL1\",\"COL2\",\"COL3\")\r\nrow.names &lt;- c(\"ROW1\",\"ROW2\",\"ROW3\")\r\nmatrix.names &lt;- c(\"Matrix1\",\"Matrix2\")<\/pre>\n<p><strong>Code Display:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Accessing-R-array-elements.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-58691\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Accessing-R-array-elements.jpg\" alt=\"Accessing R array elements\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Accessing-R-array-elements.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Accessing-R-array-elements-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Accessing-R-array-elements-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Accessing-R-array-elements-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Accessing-R-array-elements-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Accessing-R-array-elements-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p><strong># Now, we will take these vectors as input to the array.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">result &lt;- array(c(vector1,vector2),dim = c(3,3,2), dimnames = list(row.names,column.names,\r\n\u00a0\u00a0matrix.names))\r\nprint(result)<\/pre>\n<p><strong>When we execute the above code, it produces the following result:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Accessing-R-array-elements-2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-58692\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Accessing-R-array-elements-2.jpg\" alt=\"Accessing R array elements\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Accessing-R-array-elements-2.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Accessing-R-array-elements-2-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Accessing-R-array-elements-2-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Accessing-R-array-elements-2-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Accessing-R-array-elements-2-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Accessing-R-array-elements-2-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p><strong># Print the third row of the second matrix of the array.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">print(result[3,,2])<\/pre>\n<p><strong># Print the element in the 1st row and 3rd column of the 1st matrix.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">print(result[1,3,1])<\/pre>\n<p><strong># Print the 2nd Matrix.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">print(result[,,2])<\/pre>\n<p><strong>When we execute the above code, it produces the following result:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Accessing-R-array-elements-print-code.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-58693\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Accessing-R-array-elements-print-code.jpg\" alt=\"Accessing R array elements print code\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Accessing-R-array-elements-print-code.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Accessing-R-array-elements-print-code-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Accessing-R-array-elements-print-code-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Accessing-R-array-elements-print-code-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Accessing-R-array-elements-print-code-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Accessing-R-array-elements-print-code-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p><em><strong>Have you checked &#8211; <a href=\"https:\/\/data-flair.training\/blogs\/r-numeric-and-character-functions\/\">Numeric and Character Functions in R<\/a><\/strong><\/em><\/p>\n<h3>3. Manipulating R Array Elements<\/h3>\n<p><span style=\"font-weight: 400\">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.<\/span><\/p>\n<p><strong># Create two vectors of different lengths.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">vector1 &lt;- c(1,2,3)\r\nvector2 &lt;- c(3,4,5,6,7,8)<\/pre>\n<p><strong># Take these vectors as input to the array.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">array1 &lt;- array(c(vector1,vector2),dim = c(3,3,2))<\/pre>\n<p><strong># Create two vectors of different lengths.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">vector3 &lt;- c(3,2,1)\r\nvector4 &lt;- c(8,7,6,5,4,3)\r\narray2 &lt;- array(c(vector1,vector2),dim = c(3,3,2))<\/pre>\n<p><strong>Code Display:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Manipulating-R-array-elements.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-58695\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Manipulating-R-array-elements.jpg\" alt=\"Manipulating R array elements\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Manipulating-R-array-elements.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Manipulating-R-array-elements-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Manipulating-R-array-elements-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Manipulating-R-array-elements-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Manipulating-R-array-elements-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Manipulating-R-array-elements-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p><strong># create matrices from these arrays.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">matrix1 &lt;- array1[,,2]\r\nmatrix2 &lt;- array2[,,2]<\/pre>\n<p><strong># Add the matrices.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">result &lt;- matrix1+matrix2\r\nprint(result)<\/pre>\n<p><b>When we execute the above code, it produces the following result:<\/b><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Manipulating-R-array-elements-2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-58696 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Manipulating-R-array-elements-2.jpg\" alt=\"Manipulating R array elements Output\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Manipulating-R-array-elements-2.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Manipulating-R-array-elements-2-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Manipulating-R-array-elements-2-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Manipulating-R-array-elements-2-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Manipulating-R-array-elements-2-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Manipulating-R-array-elements-2-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p><em><strong>Don&#8217;t forget to check the <a href=\"https:\/\/data-flair.training\/blogs\/r-matrix-functions\/\">R Matrix Functions Tutorial<\/a><\/strong><\/em><\/p>\n<h3>4. Calculations across R Array Elements<\/h3>\n<p><span style=\"font-weight: 400\">We\u00a0will be using the <strong>apply()function<\/strong>\u00a0for calculations in an array in <a href=\"https:\/\/en.wikipedia.org\/wiki\/R_(programming_language)\">R<\/a>.<\/span><\/p>\n<p><strong>Syntax<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">apply(x, margin, fun)<\/pre>\n<p><span style=\"font-weight: 400\"><strong>Following is the description of the parameters used<\/strong> \u2212<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">x is an array.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">a margin is the name of the dataset used.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">fun is the function to be applied to the elements of the array.<\/span><\/li>\n<\/ul>\n<p><b>For Example<\/b><span style=\"font-weight: 400\">:<\/span><\/p>\n<p><span style=\"font-weight: 400\">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.<\/span><\/p>\n<p><strong># We will create two vectors of different lengths.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">vector1 &lt;- c(1,2,3)\r\nvector2 &lt;- c(3,4,5,6,7,8)<\/pre>\n<p><strong># Now, we will take these vectors as input to the array.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">new.array &lt;- array(c(vector1,vector2),dim = c(3,3,2))\r\nprint(new.array)<\/pre>\n<p><strong># Use apply to calculate the sum of the rows across all the matrices.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">result &lt;- apply(new.array, c(1), sum)\r\nprint(result)<\/pre>\n<p><span style=\"font-weight: 400\"><strong>When we execute the above code, it produces the following result:<\/strong><\/span><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Calculation-across-R-array-elements.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-58700 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Calculation-across-R-array-elements.jpg\" alt=\"Calculation across R array elements Output\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Calculation-across-R-array-elements.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Calculation-across-R-array-elements-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Calculation-across-R-array-elements-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Calculation-across-R-array-elements-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Calculation-across-R-array-elements-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Calculation-across-R-array-elements-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<h3>Extending Array Operations<\/h3>\n<p>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.<\/p>\n<h3>Advanced Array Operations and Performance Optimization<\/h3>\n<p>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.<\/p>\n<p>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.<\/p>\n<h2>Summary<\/h2>\n<p>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.<\/p>\n<p><em><strong>Time to check your knowledge of R with the <a href=\"https:\/\/data-flair.training\/blogs\/r-programming-quiz-part-1\/\">R Programming Online Quiz<\/a>.<\/strong><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":58726,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[20074,11154,20075,20076],"class_list":["post-4860","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-r","tag-create-array-in-r","tag-r-array","tag-r-array-functions","tag-r-array-operations"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>R Array Function and Create Array in R - An Ultimate Cheat Sheet - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn what is R array, how to create array in R, various functions in array and multiple operations that are performed on rows and columns in it.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/r-array\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"R Array Function and Create Array in R - An Ultimate Cheat Sheet - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn what is R array, how to create array in R, various functions in array and multiple operations that are performed on rows and columns in it.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/r-array\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2017-12-05T09:56:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-05-08T16:04:44+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/R-Array-Function-04.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"R Array Function and Create Array in R - An Ultimate Cheat Sheet - DataFlair","description":"Learn what is R array, how to create array in R, various functions in array and multiple operations that are performed on rows and columns in it.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/r-array\/","og_locale":"en_US","og_type":"article","og_title":"R Array Function and Create Array in R - An Ultimate Cheat Sheet - DataFlair","og_description":"Learn what is R array, how to create array in R, various functions in array and multiple operations that are performed on rows and columns in it.","og_url":"https:\/\/data-flair.training\/blogs\/r-array\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2017-12-05T09:56:33+00:00","article_modified_time":"2024-05-08T16:04:44+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/R-Array-Function-04.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/r-array\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/r-array\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89"},"headline":"R Array Function and Create Array in R &#8211; An Ultimate Cheat Sheet","datePublished":"2017-12-05T09:56:33+00:00","dateModified":"2024-05-08T16:04:44+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/r-array\/"},"wordCount":922,"commentCount":4,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/r-array\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/R-Array-Function-04.jpg","keywords":["Create Array in R","R array","R Array Functions","R Array Operations"],"articleSection":["R Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/r-array\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/r-array\/","url":"https:\/\/data-flair.training\/blogs\/r-array\/","name":"R Array Function and Create Array in R - An Ultimate Cheat Sheet - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/r-array\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/r-array\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/R-Array-Function-04.jpg","datePublished":"2017-12-05T09:56:33+00:00","dateModified":"2024-05-08T16:04:44+00:00","description":"Learn what is R array, how to create array in R, various functions in array and multiple operations that are performed on rows and columns in it.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/r-array\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/r-array\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/r-array\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/R-Array-Function-04.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/R-Array-Function-04.jpg","width":802,"height":420,"caption":"R Array Function"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/r-array\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"R Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/r\/"},{"@type":"ListItem","position":3,"name":"R Array Function and Create Array in R &#8211; An Ultimate Cheat Sheet"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"The DataFlair Team provides industry-driven content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our expert educators focus on delivering value-packed, easy-to-follow resources for tech enthusiasts and professionals.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam2\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/4860","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=4860"}],"version-history":[{"count":13,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/4860\/revisions"}],"predecessor-version":[{"id":136996,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/4860\/revisions\/136996"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/58726"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=4860"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=4860"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=4860"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}