[R] Calculating Summaries for each level of a Categorical variable

RaoulD raoul.t.dsouza at gmail.com
Sat Jun 26 14:17:34 CEST 2010


Hi,

I have a dataset which has a categorical variable "R",a count variable C
(integer) and 4 or more numeric variables (A,T,W,H - integers) containing
measures for "R". I would like to summarize each level of the variable R by
the average for A,T,W and H. 

I have written a function to calculate weighted averages using C as the
weight and this is given below. The function works perfectly but how do I
add the additional dimension I require to this function?

Dataset: RT=
R     A  T   W   H
R1   10 20 20  10
R2   60 20 50  10
R3   45 10 20  50
R4   68 50 20  10
R1   73 20 40  46
R3   25 30 10  54
R3   36 90 20  10
R2   29 10 30  30

# FUNCTION TO CALCULATE THE WEIGHTED AVERAGE FOR A WEIGHTED BY C
WA<-function(A,C) {
         sp_A<-c(A %*% C)
         sum_C<-sum(C)
         WA<-sp_A/sum_C   
         return(WA)      
         }

I am trying to incorporate the additional step of calculating the weighted
average of A,T,W and H for each level of R. Need help with this.

Thanks in advance!
Raoul
-- 
View this message in context: http://r.789695.n4.nabble.com/Calculating-Summaries-for-each-level-of-a-Categorical-variable-tp2269349p2269349.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list