rguides

How to Calculate Row-Wise Statistics in R

Calculate row-wise statistics when each observation needs a summary value computed from its own columns — a student’s average across exams, a product total across sales channels, or a row maximum for outlier detection. R’s vectorized functions work down columns by default, so you need to explicitly tell R to work across columns instead. For performance, base R’s rowMeans() and rowSums() are the fastest option — they are vectorized C functions that outperform any R-level loop. Use apply(df, 1, f) only when the statistic you need is not covered by one of the built-in row functions.

df <- data.frame(
  math = c(85, 92, 78),
  science = c(90, 88, 82),
  english = c(88, 85, 79)
)

df$avg <- rowMeans(df)
df$total <- rowSums(df)
df$max <- apply(df, 1, max)

rowMeans() and rowSums() are significantly faster than apply(df, 1, mean) on large data frames. For more complex aggregations, dplyr::rowwise() pairs with c_across():

library(dplyr)

df %>%
  rowwise() %>%
  mutate(avg = mean(c_across(everything())))

Both approaches handle NAs, but you must opt in. rowMeans() and rowSums() accept na.rm = TRUE, and rowwise() passes na.rm through to each summary function:

df$avg <- rowMeans(df[, c("math", "science")], na.rm = TRUE)

If you do not set na.rm = TRUE and any column has NA, the result for that row is also NA. Use complete.cases(df) first if you want to exclude rows with any missing value before computing statistics.

See also

  • apply(), Apply functions over margins
  • mean(), Calculate arithmetic mean