You can use apply()
or, as @akrun mentioned in a comment, colMeans()
. The latter is optimized for this situation so it will likely perform better than the former for large datasets.
You mentioned that you have data of multiple types and you want to select only numeric columns. That's easy enough, you just have to identify the numeric columns beforehand. That can be done using sapply()
with is.numeric()
.
# Select numeric columns
data.numcols <- data[, sapply(data, is.numeric)]
# Using apply
all.means <- apply(data.numcols, 2, mean)
# Using colMeans
all.means <- colMeans(data.numcols)
If your columns contain NA
, you can exclude NA
values like so:
# Using apply
all.means <- apply(data.numcols, 2, function(x) mean(x, na.rm = TRUE))
# Using colMeans
all.means <- colMeans(data.numcols, na.rm = TRUE)