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:
fac < - c( 1 , 1 , 1 , 1 , 2 , 2 , 2 , 3 , 3 )
vec < - c( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 )
tapply(vec, fac, sum )
|
Output:
1 2 3
10 18 17
This is how above code works:

Example 2:
fac < - c( 1 , 1 , 1 , 1 , 2 , 2 , 2 , 3 , 3 )
vec < - c( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 )
tapply(vec, fac, prod)
|
Output:
1 2 3
24 210 72
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
19 Jun, 2020
Like Article
Save Article