[R] applying math/stat functions to rows in data frame

Robert A LaBudde ral at lcfltd.com
Sat Sep 15 18:09:38 CEST 2007


At 12:02 PM 9/15/2007, Gerald wrote:
>Hi All,
>
>There are a variety of functions that can be applied to a variable
>(column) in a data frame: mean, min, max, sd, range, IQR, etc.
>
>I am aware of only two that work on the rows, using q1-q3 as example
>variables:
>
>rowMeans(cbind(q1,q2,q3),na.rm=T)   #mean of multiple variables
>rowSums (cbind(q1,q2,q3),na.rm=T)   #sum of multiple variables
>
>Can the standard column functions (listed in the first sentence) be
>applied to rows, with the use of correct indexes to reference the
>columns of interest?  Or, must these summary functions be programmed
>separately to work on a row?

Try using t() to transpose the matrix, and then apply the column 
function of interest.

================================================================
Robert A. LaBudde, PhD, PAS, Dpl. ACAFS  e-mail: ral at lcfltd.com
Least Cost Formulations, Ltd.            URL: http://lcfltd.com/
824 Timberlake Drive                     Tel: 757-467-0954
Virginia Beach, VA 23464-3239            Fax: 757-467-2947

"Vere scire est per causas scire"



More information about the R-help mailing list