Open In App

Apply a Function over a Ragged Array in R Programming – tapply() Function

Last Updated : 19 Jun, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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:




# R Program to apply a function
# over a data object
  
# Creating Factor
fac <- c(1, 1, 1, 1, 2, 2, 2, 3, 3)
  
# Created Vector
vec <- c(1, 2, 3, 4, 5, 6, 7, 8, 9
  
# Calling tapply() Function
tapply(vec, fac, sum)


Output:

 1  2  3 
10 18 17 

This is how above code works:

 
Example 2:




# R Program to apply a function
# over a data object
  
# Creating Factor
fac <- c(1, 1, 1, 1, 2, 2, 2, 3, 3)
  
# Created Vector
vec <- c(1, 2, 3, 4, 5, 6, 7, 8, 9
  
# Calling tapply() Function
tapply(vec, fac, prod)


Output:

 1   2   3 
24 210  72 


Similar Reads

apply(), lapply(), sapply(), and tapply() in R
In this article, we will learn about the apply(), lapply(), sapply(), and tapply() functions in the R Programming Language. The apply() collection is a part of R essential package. This family of functions helps us to apply a certain function to a certain data frame, list, or vector and return the result as a list or vector depending on the functio
4 min read
Perform Operations over Margins of an Array or Matrix in R Programming - apply() Function
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: # R program to illustrate # apply() function # Creating a matrix A = matrix(1:
2 min read
Apply a Function over a List of elements in R Programming - lapply() Function
lapply() function in R Programming Language is used to apply a function over a list of elements. lapply() function is used with a list and performs the following operations: lapply(List, length): Returns the length of objects present in the list, List.lapply(List, sum): Returns the sum of elements held by objects in the list, List.lapply(List, mean
2 min read
Recursively apply a Function to a List in R Programming - rapply() function
rapply() function in R Language is used to recursively apply a function to a list. Syntax: rapply(object, f, classes = "ANY", deflt = NULL, how = c("unlist", "replace", "list")) Parameters: object: represents list or an expression f: represents function to be applied recursively classes: represents class name of the vector or "ANY" to match any of
3 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 each element of x Example 1: # R program to illustr
1 min read
Compute the value of Quantile Function over F Distribution in R Programming - qf() Function
qf() function in R Language is used to compute the value of quantile function over F distribution for a sequence of numeric values. It also creates a density plot of quantile function over F Distribution. Syntax: qf(x, df1, df2) Parameters: x: Numeric Vector df: Degree of Freedom Example 1: # R Program to compute value of # Quantile Function over F
1 min read
Compute the Value of Quantile Function over Weibull Distribution in R Programming - qweibull() Function
qweibull() function in R Language is used to compute the value of Quantile Function for Weibull Distribution. Syntax: qweibull(x, shape) Parameters: x: Numeric Vector shape: Shape Parameter Example 1: # R Program to compute the value of # Quantile Weibull Function # Creating a sequence of x-values x &lt;- seq(0, 1, by = 0.2) # Calling qweibull() Fu
1 min read
Compute the value of Quantile Function over Studentized Distribution in R Programming - qtukey() Function
qtukey() function in R Language is used to compute the value of Studentized Range Quantile Function over a sequence of Numeric Values. Syntax: qtukey(x, nmeans, df) Parameters: x: Numeric Vector nmeans: Number of means df: Degree of Freedom Example 1: # R Program to compute the value of # Quantile Function Studentized Range # Creating a sequence of
1 min read
Compute the value of Quantile Function over Wilcoxon Signedrank Distribution in R Programming - qsignrank() Function
qsignrank() function in R Language is used to compute the value of Quantile Function(QF) over Wilcoxon Signedrank Statistic Distribution for a sequence of Numeric values. Syntax: qsignrank(x, n) Parameters: x: Numeric Vector n: Sample Size Example 1: # R Program to compute the value of # QF over Wilcoxon Signedrank Distribution # Creating a sequenc
1 min read
Compute the value of Quantile Function over Wilcoxon Rank Sum Distribution in R Programming – qwilcox() Function
qwilcox() function in R Language is used to compute the value of Quantile Function(QF) for Wilcoxonrank Sum Statistic Distribution over a sequence of Numeric Values. Syntax: qwilcox(x, m, n) Parameters: x: Numeric Vector m: Larger sample size n: Smaller sample size Example 1: # R Program to compute the value of # QF over Wilcoxonrank Sum Distributi
1 min read