tapply()
In this page:
tapply()
tapply(x, index, FUN) applies a function to subsets of a vector, grouped by a second vector (the index), similar in spirit to aggregate() but working directly on vectors rather than data frames. It's a compact way to compute a per-group statistic without building a full data frame first. The result is an array indexed by the distinct group values.
Note: tapply() is the vector-based equivalent of aggregate() -- reach for it when you don't already have a data frame.
Example: tapply()
scores <- c(90, 85, 70, 60, 95)
groups <- c("A", "A", "B", "B", "A")
averages <- tapply(scores, groups, mean)
print(averages)
Login to try C/C++/Java/PHP code in the editor