Rscript -e 'df <- read.csv("data.csv"); print(colMeans(df[c("age","salary","score")]))'

Category: Data Science & Analysis

Rscript -e 'df <- read.csv("data.csv"); print(colMeans(df[c("age","salary","score")]))'

Average several numeric columns at once

colMeans() computes the mean of every column in a data frame or matrix in one call. The df[c(...)] subsetting selects just the three numeric columns. The result is a named vector of column means, skipping any non-numeric columns you leave out. Use colSums() for totals or apply(df, 2, median) for per-column medians.
Looking for more? Search all 7,657 commands — works offline, in English or Spanish, and fixes typos.