Why NOT the accepted answer?
The accepted answer is correct, however, it is too specific to this particular task and impossible to be generalized. What if we need, instead of mean
, other statistics like var
, skewness
, etc. , or even a custom function?
A more flexible solution:
row_means <- apply(X=data, MARGIN=1, FUN=mean, na.rm=TRUE)
More details on apply
:
Generally, to apply any function (custom or built-in) on the entire dataset, column-wise or row-wise, apply
or one of its variations (sapply
, lapply`, ...) should be used. Its signature is:
apply(X, MARGIN, FUN, na.rm)
where:
X
: The data of form dataframe or matrix.
MARGIN
: The dimension on which the aggregation takes place. Use 1
for row-wise operation and 2
for column-wise operation.
FUN
: The operation to be called on the data. Here any pre-defined R functions, as well as any user-defined function could be used.
na.rm
: If TRUE
, the NA
values will be removed before FUN
is called.
Why should I use apply
?
For many reasons, including but not limited to:
- Any function can be easily plugged in to
apply
.
- For different preferences such as the input or output data types, other variations can be used (e.g.,
lapply
for operations on lists).
- (Most importantly) It facilitates scalability since there are versions of this function that allows parallel execution (e.g.
mclapply
from {parallel}
library). For instance, see [+] or [+].