apply a function over groups of columns

This may be more generalizable to your situation in that you pass a list of indices. If speed is an issue (large data frame) I’d opt for lapply with do.call rather than sapply: x <- list(1:3, 4:6) do.call(cbind, lapply(x, function(i) rowMeans(dat[, i]))) Works if you just have col names too: x <- list(c(‘a’,’b’,’c’), c(‘d’, ‘e’, … Read more