← Back to R Course | Chapter 11: Apply Family | Lesson 4 of 6

tapply()

In this page:

  1. tapply()

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()

markup
scores <- c(90, 85, 70, 60, 95)
groups <- c("A", "A", "B", "B", "A")

averages <- tapply(scores, groups, mean)
print(averages)
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.