

{"id":4314,"date":"2017-09-23T06:32:23","date_gmt":"2017-09-23T01:02:23","guid":{"rendered":"http:\/\/data-flair.training\/blogs\/?p=4314"},"modified":"2021-08-25T17:26:52","modified_gmt":"2021-08-25T11:56:52","slug":"r-matrix-functions","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/r-matrix-functions\/","title":{"rendered":"Matrix Function in R &#8211; Master the apply() and sapply() functions in R"},"content":{"rendered":"<p>In this tutorial, we are going to cover the functions that are applied to the matrices in R i.e. apply() and sapply() function. Also, we will see how to use these functions of the R matrix with the help of examples.<\/p>\n<p><em><strong>I hope you have completed the <a href=\"https:\/\/data-flair.training\/blogs\/r-matrix-operations-applications\/\">R Matrix tutorial<\/a>, before proceeding ahead!<\/strong><\/em><\/p>\n<p>So, let&#8217;s start exploring matrix functions in R.<\/p>\n<h2>What is R Matrix and Matrix Function in R?<\/h2>\n<p>First of all, let&#8217;s revise what\u00a0are matrices. A matrix is a two-dimensional rectangular data set. Thus it can be created using vector input into the matrix function. Also, a<em> matrix is a collection of numbers arranged into a fixed number of rows and <\/em><em>columns<\/em><strong>.<\/strong> The numbers present in the matrix are real numbers. We then carry out the memory replication of the matrix using\u00a0 the matrix function. Hence, the data elements must be of the same basic type. Matrices functions are those functions which we use in matrices.<\/p>\n<p>There are two types of matrix function in R:<\/p>\n<ul>\n<li>apply()<\/li>\n<li>sapply()<\/li>\n<\/ul>\n<p>In the above paragraph, we have discussed about the R matrix. Now let&#8217;s proceed to detail understanding of the types of matrix function in R.<\/p>\n<p><em><strong>You must learn about <a href=\"http:\/\/data-flair.training\/blogs\/r-vector-types-and-operations\/\">Vector Operations in R<\/a><\/strong><\/em><\/p>\n<h3>What is apply() function in R?<\/h3>\n<p>Let&#8217;s now understand the R apply() function and its usage with examples.<\/p>\n<h4>1. apply() function in R<\/h4>\n<p>It applies functions over array margins. It returns a vector or array or list of values obtained by applying a function to margins of an array or matrix.<\/p>\n<ul>\n<li><strong>Keywords &#8211;\u00a0<\/strong>array, iteration<\/li>\n<li><strong>Usage &#8211;\u00a0<\/strong>apply(X, MARGIN, FUN, \u2026)<\/li>\n<\/ul>\n<p><strong>Arguments &#8211; <\/strong>The arguments for the apply function in R are explained below:<\/p>\n<ul>\n<li><strong> X &#8211;\u00a0<\/strong>an array, including a matrix.<\/li>\n<li><strong> \u2026 &#8211;\u00a0<\/strong>optional arguments to FUN.<\/li>\n<li><strong>FUN &#8211;\u00a0<\/strong>The function to apply: see \u2018Details\u2019.<\/li>\n<li><strong>MARGIN &#8211;\u00a0<\/strong>Functions will apply on subscripts in a vector.<\/li>\n<\/ul>\n<p><strong>For example &#8211;<\/strong>\u00a0A matrix 1 indicates rows, matrix 2 indicates columns, matrix c(1, 2) indicates rows and columns. In this, X is named\u00a0dimnames and it can be a character vector selecting dimension names.<\/p>\n<h4>2. The apply() family<\/h4>\n<p>Apply functions are a family of functions in base R, which allow us to perform actions on many chunks of data. An apply function is a loop, but it runs faster than loops and often with less code. And, there are different\u00a0apply() functions.<\/p>\n<p>The called function could be:<\/p>\n<ul>\n<li>Some aggregating function which includes meaning, or the sum (including return a number or scalar).<\/li>\n<li>Other transforming or subsetting functions.<\/li>\n<li>Some vectorized functions which return more complex structures like<em> lists, vectors, matrices, and arrays.<\/em><\/li>\n<\/ul>\n<p>We can perform operations with very few lines of code in apply().<\/p>\n<p><em><strong>Have you checked &#8211; <a href=\"https:\/\/data-flair.training\/blogs\/r-array\/\">R Array Function<\/a><\/strong><\/em><\/p>\n<h4>3. How to use apply() function in R?<\/h4>\n<p>So, let us start with apply(), which operates on arrays:<\/p>\n<h5>3.1 apply function in R examples<\/h5>\n<p>my.matrx is a matrix with <em>1-5 in column 1, 6-10 in column 2, and 11-15 in column 3<\/em>. my.matrx is used to show some of the basic uses of the apply function.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">my.matrx &lt;- matrix(c(1:5, 6:10, 11:15),\u00a0nrow\u00a0= 5,\u00a0ncol\u00a0= 3)\r\nmy.matrx<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/matrx-function-output.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59217\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/matrx-function-output.jpg\" alt=\"matrx function output - R Matrix Function\" width=\"1366\" height=\"535\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/matrx-function-output.jpg 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/matrx-function-output-150x59.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/matrx-function-output-300x117.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/matrx-function-output-768x301.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/matrx-function-output-1024x401.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/matrx-function-output-520x204.jpg 520w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p><strong>Example 1: Using apply to find row sums<\/strong><\/p>\n<p>We will summarize the data in matrix m by finding the sum of each row. The arguments are; X = m, MARGIN = 1 (for\u00a0row), and FUN = sum.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">apply(my.matrx, 1, sum)<\/pre>\n<p>It will return a vector containing the sums for each row.<\/p>\n<p><strong>Example 2: Creating a function in the arguments<\/strong><\/p>\n<p>Now, we will find how many data points (n) are in each column of m by using columns, MARGIN = 2. Thus, we can use the length function to do this.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">apply(my.matrx, 2, length)<\/pre>\n<p>There isn\u2019t a function in <a href=\"https:\/\/www.r-project.org\/about.html\">R<\/a> to find n-1 for each column. So, if we want to create our own function and if the function is simple, you can create it right inside the arguments for applying. In the arguments, we created a function that returns length &#8211; 1.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">apply(my.matrx, 2, function (x) length(x)-1)<\/pre>\n<p>As we have seen, the function returned a vector of n-1 for each column.<\/p>\n<p><strong>Example 3: Transforming data<\/strong><\/p>\n<p>In the previous examples, we used apply\u00a0to summarize over a row or column. We can also use apply to repeat a function on cells within a matrix. Now, in this example, we will learn how to use apply function to transform the values in each cell. Please give more attention to the MARGIN argument.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">my.matrx2 &lt;- apply(my.matrx,1:2, function(x) x+3).\r\nmy.matrx2<\/pre>\n<p><strong>Code Display:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/apply-my-matrx-input.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59218\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/apply-my-matrx-input.jpg\" alt=\"apply my matrx input - R Matrix Function\" width=\"1366\" height=\"728\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/apply-my-matrx-input.jpg 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/apply-my-matrx-input-150x80.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/apply-my-matrx-input-300x160.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/apply-my-matrx-input-768x409.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/apply-my-matrx-input-1024x546.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/apply-my-matrx-input-520x277.jpg 520w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/apply-my-matrx-output.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59219\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/apply-my-matrx-output.jpg\" alt=\"apply my matrx output\" width=\"1366\" height=\"728\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/apply-my-matrx-output.jpg 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/apply-my-matrx-output-150x80.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/apply-my-matrx-output-300x160.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/apply-my-matrx-output-768x409.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/apply-my-matrx-output-1024x546.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/apply-my-matrx-output-520x277.jpg 520w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p><strong>Example 4: Vectors<\/strong><\/p>\n<p>In previous examples, we have learned several ways to use the apply function on a matrix. But what if we want to loop it through a vector instead? Will the apply function work? Let&#8217;s check.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">vec\u00a0&lt;- c(1:5)\r\nvec<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/Example-of-Vectors.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59220\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/Example-of-Vectors.jpg\" alt=\"Example of Vectors - R Vector Function\" width=\"1366\" height=\"728\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/Example-of-Vectors.jpg 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/Example-of-Vectors-150x80.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/Example-of-Vectors-300x160.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/Example-of-Vectors-768x409.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/Example-of-Vectors-1024x546.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/Example-of-Vectors-520x277.jpg 520w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p>When we will run this function, it will return the error: Error in apply(v, 1, sum): dim(X) must have a positive length. As we can see, this didn\u2019t work because apply was expecting the data to have at least two dimensions. If we are using data in a vector, we need to use\u00a0lapply,\u00a0sapply, or\u00a0vapply\u00a0instead.<\/p>\n<p>Any doubts in R Matrix Function till now? Please comment below.<\/p>\n<h3>What is sapply() function in R?<\/h3>\n<p>It is a dimension preserving variant of &#8220;sapply&#8221; and &#8220;lapply&#8221;.<\/p>\n<p>sapply\u00a0is a\u00a0user-friendly\u00a0version and is a wrapper of\u00a0lapply. By default, sapply\u00a0returns a vector, matrix or an array.<\/p>\n<ul>\n<li><strong>Keywords &#8211;\u00a0<\/strong>Misc, utilities<\/li>\n<li><strong>Usage &#8211;\u00a0<\/strong>Sapply(X, FUN, &#8230;, simplify = TRUE, USE.NAMES = TRUE)<br \/>\nLapply(X, FUN, &#8230;)<\/li>\n<\/ul>\n<p><strong>Arguments &#8211;<\/strong> The arguments used in the sapply() function are discussed below:<\/p>\n<ul>\n<li><strong> X &#8211;\u00a0<\/strong>It is a vector or list to call\u00a0sapply.<\/li>\n<li><strong> FUN &#8211; <\/strong>A\u00a0function.<\/li>\n<li><strong> &#8230; &#8211; <\/strong>Optional arguments to FUN.<\/li>\n<li><strong> simplify &#8211;\u00a0<\/strong>It is a logical value which defines whether a result is simplified to a vector or matrix (if possible).<\/li>\n<li><strong> USE.NAMES &#8211; <\/strong>Logical; if it is TRUE and X is a character, then use X as names for the result unless it has names already.<\/li>\n<\/ul>\n<p><em><strong>Do you know about <a href=\"http:\/\/data-flair.training\/blogs\/r-lists-tutorial\/\">R Lists<\/a><\/strong><\/em><\/p>\n<h3>How to use\u00a0sapply() function in R?<\/h3>\n<p>sapply() function applies a function to margins of an array or matrix.<\/p>\n<p><strong>Usage &#8211;\u00a0<\/strong>sapply(x,\u00a0func, &#8230;, simplify = TRUE, USE.NAMES = TRUE)<\/p>\n<p><strong>sapply function in R Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; BOD\u00a0\u00a0\u00a0 \u00a0#R built-in dataset, Biochemical Oxygen Demand<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/BOD-Time-demand.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-63146\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/BOD-Time-demand.jpg\" alt=\"BOD Time demand\" width=\"1299\" height=\"735\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/BOD-Time-demand.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/BOD-Time-demand-150x85.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/BOD-Time-demand-300x170.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/BOD-Time-demand-768x435.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/BOD-Time-demand-1024x579.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/BOD-Time-demand-520x294.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p>Sum up for each row:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">sapply(BOD, sum)<\/pre>\n<p>Multiply all values by 10:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;\u00a0sapply(BOD,function(x) 10 * x)<\/pre>\n<p>Used for array, margin set to 1:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">x &lt;- array(1:9)\r\n&gt;\u00a0sapply(x,function(x) x * 10)<\/pre>\n<p>Two-dimension array, the margin can be 1 or 2:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; x &lt;- array(1:9,c(3,3))\r\n&gt; x\r\n&gt;\u00a0sapply(x,function(x) x * 10)\r\n&gt; sapply(c(1:3), function(x) x^2)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/BOD-Time-demand-sapply.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-63145\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/BOD-Time-demand-sapply.jpg\" alt=\"BOD Time demand sapply\" width=\"1299\" height=\"1252\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/BOD-Time-demand-sapply.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/BOD-Time-demand-sapply-150x145.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/BOD-Time-demand-sapply-300x289.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/BOD-Time-demand-sapply-768x740.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/BOD-Time-demand-sapply-1024x987.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/BOD-Time-demand-sapply-520x501.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<h2>Summary<\/h2>\n<p>We have studied about R matrix function in detail. Also, we discussed its most promising uses, examples and how the function is applied over datatypes. Moreover, in this tutorial, we have discussed the two matrix function in R; apply() and sapply() with its usage and examples. Hence, the information which we have discussed in this tutorial is sufficient enough to learn matrices and its functions in R.<\/p>\n<p>Still, if you have any query or suggestions related to this matrix function in R, feel free to share with us in the comment section.<\/p>\n<p><em><strong>Now, its turn for exploring the <a href=\"https:\/\/data-flair.training\/blogs\/r-recursive-function\/\">R recursive function tutorial<\/a>.<\/strong><\/em><span hidden class=\"__iawmlf-post-loop-links\" data-iawmlf-links=\"[{&quot;id&quot;:1463,&quot;href&quot;:&quot;https:\\\/\\\/www.r-project.org\\\/about.html&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20251001184431\\\/https:\\\/\\\/www.r-project.org\\\/about.html&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2025-12-09 07:45:55&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-13 16:47:38&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-17 00:44:38&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-20 07:22:29&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-24 11:16:29&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2025-12-27 15:20:48&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-03 17:17:05&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-07 10:13:13&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-10 21:41:26&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-15 04:43:55&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-19 10:33:02&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-23 04:45:14&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-26 07:29:46&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-29 17:22:16&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-02 13:19:23&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-06 04:11:53&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-10 17:03:32&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-02-14 06:52:53&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-17 15:21:44&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-02-21 02:00:26&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-24 08:01:42&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-27 12:29:53&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-02 14:58:03&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-05 20:44:49&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-10 04:28:05&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-13 05:33:57&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-16 05:53:50&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-21 02:54:48&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-24 07:39:50&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-28 15:59:49&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-04-01 09:45:43&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-05 15:11:43&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-09 04:11:17&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-12 14:44:40&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-04-16 05:31:41&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-04-19 11:55:27&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-04-23 03:08:31&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-26 07:10:31&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-29 16:46:00&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-02 19:54:38&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-06 20:30:02&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-10 10:01:23&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-14 12:31:42&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-17 21:24:47&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-21 13:32:11&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-24 18:37:43&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-28 04:36:06&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-06-01 14:14:54&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-06-04 20:45:27&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-06-08 04:11:15&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-06-11 14:43:53&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-06-14 19:16:45&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-06-18 13:15:00&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-06-22 13:29:20&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-06-25 13:30:21&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-06-29 01:50:41&quot;,&quot;http_code&quot;:206}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-06-29 01:50:41&quot;,&quot;http_code&quot;:206},&quot;process&quot;:&quot;done&quot;}]\"><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we are going to cover the functions that are applied to the matrices in R i.e. apply() and sapply() function. Also, we will see how to use these functions of the&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":59222,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[16699,20123,11226,20124],"class_list":["post-4314","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-r","tag-matrix-function-in-r","tag-r-apply-function","tag-r-matrix","tag-r-sapply-function"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Matrix Function in R - Master the apply() and sapply() functions in R - DataFlair<\/title>\n<meta name=\"description\" content=\"R matrix function tutorial covers matrix functions in R; apply function and sapply function with uses and examples to understand the concept thoroughly.\" \/>\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-matrix-functions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Matrix Function in R - Master the apply() and sapply() functions in R - DataFlair\" \/>\n<meta property=\"og:description\" content=\"R matrix function tutorial covers matrix functions in R; apply function and sapply function with uses and examples to understand the concept thoroughly.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/r-matrix-functions\/\" \/>\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-09-23T01:02:23+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-08-25T11:56:52+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/R-matrix-functions.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=\"6 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Matrix Function in R - Master the apply() and sapply() functions in R - DataFlair","description":"R matrix function tutorial covers matrix functions in R; apply function and sapply function with uses and examples to understand the concept thoroughly.","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-matrix-functions\/","og_locale":"en_US","og_type":"article","og_title":"Matrix Function in R - Master the apply() and sapply() functions in R - DataFlair","og_description":"R matrix function tutorial covers matrix functions in R; apply function and sapply function with uses and examples to understand the concept thoroughly.","og_url":"https:\/\/data-flair.training\/blogs\/r-matrix-functions\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2017-09-23T01:02:23+00:00","article_modified_time":"2021-08-25T11:56:52+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/R-matrix-functions.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":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/r-matrix-functions\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/r-matrix-functions\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89"},"headline":"Matrix Function in R &#8211; Master the apply() and sapply() functions in R","datePublished":"2017-09-23T01:02:23+00:00","dateModified":"2021-08-25T11:56:52+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/r-matrix-functions\/"},"wordCount":1129,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/r-matrix-functions\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/R-matrix-functions.jpg","keywords":["Matrix Function in R","R apply function","R matrix","R sapply function"],"articleSection":["R Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/r-matrix-functions\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/r-matrix-functions\/","url":"https:\/\/data-flair.training\/blogs\/r-matrix-functions\/","name":"Matrix Function in R - Master the apply() and sapply() functions in R - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/r-matrix-functions\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/r-matrix-functions\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/R-matrix-functions.jpg","datePublished":"2017-09-23T01:02:23+00:00","dateModified":"2021-08-25T11:56:52+00:00","description":"R matrix function tutorial covers matrix functions in R; apply function and sapply function with uses and examples to understand the concept thoroughly.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/r-matrix-functions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/r-matrix-functions\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/r-matrix-functions\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/R-matrix-functions.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/R-matrix-functions.jpg","width":802,"height":420,"caption":"R matrix functions"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/r-matrix-functions\/#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":"Matrix Function in R &#8211; Master the apply() and sapply() functions in R"}]},{"@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\/4314","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=4314"}],"version-history":[{"count":14,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/4314\/revisions"}],"predecessor-version":[{"id":63917,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/4314\/revisions\/63917"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/59222"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=4314"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=4314"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=4314"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}