Perform Operations over Margins of an Array or Matrix in R Programming - apply() Function Last Updated : 04 Jun, 2020 Comments Improve Suggest changes Like Article Like Report apply() function in R Language is used to perform mathematical operations across elements of an array or a matrix. Syntax: apply(x, margin, func) Parameters: x: Array or matrix margin: dimension on which operation is to be applied func: operation to be applied Example 1: Python3 1== # R program to illustrate # apply() function # Creating a matrix A = matrix(1:9, 3, 3) print(A) # Applying apply() over row of matrix # Here margin 1 is for row r = apply(A, 1, sum) print(r) # Applying apply() over column of matrix # Here margin 2 is for column c = apply(A, 2, sum) print(c) Output: [, 1] [, 2] [, 3] [1, ] 1 4 7 [2, ] 2 5 8 [3, ] 3 6 9 [1] 12 15 18 [1] 6 15 24 Example 2: Python3 1== # R program to illustrate # apply() function # Creating a matrix A = matrix(1:9, 3, 3) print(A) # Applying apply() over row of matrix # Here margin 1 is for row r = apply(A, 1, prod) print(r) # Applying apply() over column of matrix # Here margin 2 is for column c = apply(A, 2, prod) print(c) Output: [, 1] [, 2] [, 3] [1, ] 1 4 7 [2, ] 2 5 8 [3, ] 3 6 9 [1] 28 80 162 [1] 6 120 504 Comment More infoAdvertise with us Next Article Perform Operations over Margins of an Array or Matrix in R Programming - apply() Function N nidhi_biet Follow Improve Article Tags : R Language R Matrix-Function R Array-Functions Similar Reads Apply a Function over a Ragged Array in R Programming - tapply() Function tapply() function in R Language is used to apply a function over a subset of vectors given by a combination of factors Syntax: tapply(vector, factor, fun) Parameters: vector: Created Vector factor: Created Factor fun: Function to be applied Example 1: Python3 1== # R Program to apply a function # ov 1 min read Convert an Object into a Matrix in R Programming - as.matrix() Function The as.matrix() function within R converts objects of various classes into a matrix. This can be helpful to work with structures of various data that can be converted into the matrix structure so that it becomes easier to analyze.Syntax: as.matrix(x)Parameters: x: Object to be convertedExample 1: Co 2 min read Applying a Function over an Object in R Programming - sapply() Function sapply() function in R Language takes list, vector or data frame as input and gives output in vector or matrix. It is useful for operations on list objects and returns a list object of same length of original set. Syntax: sapply(X, FUN) Parameters: X: A vector or an object FUN: Function applied to e 1 min read Get Transpose of a Matrix or Data Frame in R Programming - t() Function t() function in R Language is used to calculate transpose of a matrix or Data Frame. Syntax: t(x) Parameters: x: matrix or data frame Example 1: Python3 # R program to illustrate # t function # Getting R Biochemical Oxygen Demand Dataset BOD # Calling t() function t(BOD) Output: Time demand 1 1 8.3 1 min read Getting a Matrix of number of rows in R Programming - row() Function In this article, we will discuss how to find the number of rows and columns in a matrix in R Programming Language. nrow() Function in Rnrow() function in R Language is used to get the row number of a matrix. Syntax: row(x, as.factor=FALSE) Parameters:x: matrixas.factor: a logical value indicating w 2 min read Like