Skip to content
Prev 68938 / 398513 Next

"apply" question

On 5/2/05, Christoph Scherber <Christoph.Scherber at uni-jena.de> wrote:
Supposing our data is called DF,

rowSums(!is.na(DF[,6:10]))
In the expression below 1 + 0 *DF[,6:10] is like DF[,6:10] except
all non-NAs are replaced by 1.  Multiplying DF[,1:5] by that
effectively replaces each element in DF[,1:5] with an NA if
the corresponding DF[,6:10] contained an NA.

rowSums( DF[,1:5] * (1 + 0 * DF[,6:10]), na.rm = TRUE )
I guess you meant sum when you referred to mean in (2).  If you really
do want the mean replace rowSums with rowMeans in the expression
given above in the answer to (2).