How to calculate row-wise statistics in R

· 1 min read · Updated March 14, 2026 · beginner
r statistics row-wise dplyr

Row-wise operations compute statistics across columns for each row.

With dplyr

library(dplyr)

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

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

With base R

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

With data.table

library(data.table)

dt <- data.table(math = c(85, 92, 78), science = c(90, 88, 82))
dt[, avg := rowMeans(.SD), .SDcols = c("math", "science")]

See Also

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