{"id":1759,"date":"2020-12-05T16:09:23","date_gmt":"2020-12-05T16:09:23","guid":{"rendered":"https:\/\/ded9.com\/?p=1759"},"modified":"2025-11-05T10:07:42","modified_gmt":"2025-11-05T10:07:42","slug":"arrays-in-r-programming-language-in-simple-language","status":"publish","type":"post","link":"https:\/\/ded9.com\/tr\/arrays-in-r-programming-language-in-simple-language\/","title":{"rendered":"Arrays in R Programming Language \u2014 Explained Simply"},"content":{"rendered":"<p><span style=\"font-size: 12pt;\">Arrays are data objects in the R programming language that can store data in more than two dimensions. For example, if we create an array of dimensions (2,3,4);<\/span><\/p>\n<p>This array can then be used to create four rectangular matrices, each with two rows and three columns. <a href=\"https:\/\/ded9.com\/the-basics-of-arrays-in-java\/\">Arrays<\/a> can only store data types.<span id=\"more-56195\"><\/span><\/p>\n<p>An array is created using the Array () function. This array accepts vectors as input and creates arrays; it uses values \u200b\u200bthat are <strong>dimension<\/strong> parameters.<\/p>\n<p><span style=\"font-size: 12pt;\">Example<\/span><\/p>\n<p>The following example creates an array of three matrices, each with three rows and three columns.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\"># Create two vectors of different lengths.\r\n\r\nvector1 &lt;- c (5,9,3)\r\n\r\nvector2 &lt;- c (10,11,12,13,14,15)\r\n\r\n# Take these vectors as input to the array.\r\n\r\nresult &lt;- array (c (vector1, vector2), dim = c (3,3,2))\r\n\r\nprint (result)<\/pre>\n<\/div>\n<p>When we run the above code, the following results are generated:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">,, 1\r\n\r\n[, 1] [, 2] [, 3]\r\n\r\n[1,] 5 10 13\r\n\r\n[2,] 9 11 14\r\n\r\n[3,] 3 12 15\r\n\r\n,, 2\r\n\r\n[, 1] [, 2] [, 3]\r\n\r\n[1,] 5 10 13\r\n\r\n[2,] 9 11 14\r\n\r\n[3,] 3 12 15<\/pre>\n<\/div>\n<h2>Names of rows and columns<\/h2>\n<p>We can arrange rows, columns, and matrices within an array; <strong>Assign<\/strong>\u00a0the desired name\u00a0using the\u00a0<a href=\"https:\/\/www.rdocumentation.org\/packages\/base\/versions\/3.6.2\/topics\/dimnames\" target=\"_blank\" rel=\"noopener\"><strong>dimnames<\/strong> <\/a>parameter.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\"># Create two vectors of different lengths.\r\n\r\nvector1 &lt;- c (5,9,3)\r\n\r\nvector2 &lt;- c (10,11,12,13,14,15)\r\n\r\ncolumn.names &lt;- c (\u201cCOL1 \u2033,\u201d COL2 \u2033, \u201dCOL3\u201d)\r\n\r\nrow.names &lt;- c (\u201cROW1 \u2033,\u201d ROW2 \u2033, \u201dROW3\u201d)\r\n\r\nmatrix.names &lt;- c (\u201cMatrix1 \u2033,\u201d Matrix2 \u201d)\r\n\r\n# Take these vectors as input to the array.\r\n\r\nresult &lt;- array (c (vector1, vector2), dim = c (3,3,2), dimnames = list (row.names, column.names,\r\n\r\nmatrix.names))\r\n\r\nprint (result)<\/pre>\n<\/div>\n<p>When we run the above code, the following results are generated:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">,, Matrix1\r\n\r\nCOL1 COL2 COL3\r\n\r\nROW1 5 10 13\r\n\r\nROW2 9 11 14\r\n\r\nROW3 3 12 15\r\n\r\n,, Matrix2\r\n\r\nCOL1 COL2 COL3\r\n\r\nROW1 5 10 13\r\n\r\nROW2 9 11 14\r\n\r\nROW3 3 12 15<\/pre>\n<\/div>\n<h2>Access array members<\/h2>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\"># Create two vectors of different lengths.\r\n\r\nvector1 &lt;- c (5,9,3)\r\n\r\nvector2 &lt;- c (10,11,12,13,14,15)\r\n\r\ncolumn.names &lt;- c (\u201cCOL1 \u2033,\u201d COL2 \u2033, \u201dCOL3\u201d)\r\n\r\nrow.names &lt;- c (\u201cROW1 \u2033,\u201d ROW2 \u2033, \u201dROW3\u201d)\r\n\r\nmatrix.names &lt;- c (\u201cMatrix1 \u2033,\u201d Matrix2 \u201d)\r\n\r\n# Take these vectors as input to the array.\r\n\r\nresult &lt;- array (c (vector1, vector2), dim = c (3,3,2), dimnames = list (row.names,\r\n\r\ncolumn.names, matrix.names))\r\n\r\n# Print the third row of the second matrix of the array.\r\n\r\nprint (result [3,, 2])\r\n\r\n# Print the element in the 1st row and 3rd column of the 1st matrix.\r\n\r\nprint (result [1,3,1])\r\n\r\n# Print the 2nd Matrix.\r\n\r\nprint (result [,, 2])<\/pre>\n<\/div>\n<p>When we run the above code, the following result is obtained:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">COL1 COL2 COL3\r\n\r\n3 12 15\r\n\r\n[1] 13\r\n\r\nCOL1 COL2 COL3\r\n\r\nROW1 5 10 13\r\n\r\nROW2 9 11 14\r\n\r\nROW3 3 12 15<\/pre>\n<\/div>\n<h2>Employ array members<\/h2>\n<p>As arrays are made of matrices with different dimensions, Operations on elements are performed by accessing matrix members.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\"># Create two vectors of different lengths.\r\n\r\nvector1 &lt;- c (5,9,3)\r\n\r\nvector2 &lt;- c (10,11,12,13,14,15)\r\n\r\n# Take these vectors as input to the array.\r\n\r\narray1 &lt;- array (c (vector1, vector2), dim = c (3,3,2))\r\n\r\n# Create two vectors of different lengths.\r\n\r\nvector3 &lt;- c (9,1,0)\r\n\r\nvector4 &lt;- c (6,0,11,3,14,1,2,6,9)\r\n\r\narray2 &lt;- array (c (vector1, vector2), dim = c (3,3,2))\r\n\r\n# create matrices from these arrays.\r\n\r\nmatrix1 &lt;- array1 [,, 2]\r\n\r\nmatrix2 &lt;- array2 [,, 2]\r\n\r\n# Add the matrices.\r\n\r\nresult &lt;- matrix1 + matrix2\r\n\r\nprint (result)<\/pre>\n<\/div>\n<p>When we run the above code, we get the following result:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">[, 1] [, 2] [, 3]\r\n\r\n[1,] 10 20 26\r\n\r\n[2,] 18 22 28\r\n\r\n[3,] 6 24 30<\/pre>\n<\/div>\n<h2><span style=\"font-size: 18pt;\">Computations on arrays of members<\/span><\/h2>\n<p>Using the\u00a0<strong>apply<\/strong><span style=\"box-sizing: border-box; margin: 0px; padding: 0px;\"><strong>() function,<\/strong> we can perform calculations on the\u00a0<\/span>members of an array.<\/p>\n<p><strong>Syntax<\/strong><\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">apply (x, margin, fun)<\/pre>\n<\/div>\n<p>The parameters that we write in the above code are as follows:<\/p>\n<ul>\n<li><strong>x is<\/strong>\u00a0an array.<\/li>\n<li><strong>Margin is the<\/strong>\u00a0name of the data set used.<\/li>\n<li><strong>Fun is a<\/strong>\u00a0function that must be applied to members of an array.<\/li>\n<\/ul>\n<p><strong>Example<\/strong><\/p>\n<p>In the following code, we use the apply() function to calculate the sum of the elements in the rows of all matrices in an array.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\"># Create two vectors of different lengths.\r\n\r\nvector1 &lt;- c (5,9,3)\r\n\r\nvector2 &lt;- c (10,11,12,13,14,15)\r\n\r\n# Take these vectors as input to the array.\r\n\r\nnew.array &lt;- array (c (vector1, vector2), dim = c (3,3,2))\r\n\r\nprint (new.array)\r\n\r\n# Use apply to calculate the sum of the rows across all the matrices.\r\n\r\nresult &lt;- apply (new.array, c (1), sum)\r\n\r\nprint (result)<\/pre>\n<\/div>\n<p>When we run the above code, we achieve the following results:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">,, 1\r\n\r\n[, 1] [, 2] [, 3]\r\n\r\n[1,] 5 10 13\r\n\r\n[2,] 9 11 14\r\n\r\n[3,] 3 12 15\r\n\r\n,, 2\r\n\r\n[, 1] [, 2] [, 3]\r\n\r\n[1,] 5 10 13\r\n\r\n[2,] 9 11 14\r\n\r\n[3,] 3 12 15\r\n\r\n[1] 56 68 60<\/pre>\n<h2>FAQ<\/h2>\n<div id=\"rank-math-rich-snippet-wrapper\"><div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-1\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What is an array in R?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>An array in R is a multi-dimensional data structure where all elements are of the same type, typically used for matrices or higher dimensions.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-2\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">How do I create a 3D array in R?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Use the array() function with a dim argument, e.g. my_array &lt;- array(1:24, dim = c(2,3,4)).<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-3\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">How do I access elements of an array?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Use square brackets with indices for each dimension, e.g. my_array[1,2,3] to get the element at position (1,\u20092,\u20093).<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Arrays are data objects in the R programming language that can store data in more than two dimensions. For example, if we create an array of dimensions (2,3,4); This array can then be used to create four rectangular matrices, each with two rows and three columns. Arrays can only store data types. An array is [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":1760,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[174],"tags":[3324],"class_list":["post-1759","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-r-r-studio","tag-r"],"acf":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/1759","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/comments?post=1759"}],"version-history":[{"count":5,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/1759\/revisions"}],"predecessor-version":[{"id":265013,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/1759\/revisions\/265013"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/media\/1760"}],"wp:attachment":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/media?parent=1759"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/categories?post=1759"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/tags?post=1759"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}