[R] Turning pass/fail results into a proportion

Gabor Grothendieck ggrothendieck at myway.com
Wed May 26 16:04:57 CEST 2004


Matthew Walker <m.g.walker <at> massey.ac.nz> writes:
 
: I have a data frame that contains a number of pass/fails for certain 
: variable sizes.  From that, I would like to form another data frame that 
: contains the proportions of pass/fails per variable.
: 
: So, for example:
: df <- data.frame( Var=c(3,3,3,4,4), 
: Result=c("pass","fail","fail","pass","pass"), SampleSize=c(3,3,3,2,2))
: 
: And I'd like to produce the equivalent of:
: data.frame( Var=c(3,4), ProportionPass=c(0.33, 1) )


Try using aggregate like this:

 aggregate( data.frame(ProportionPass = as.numeric(df$Result)-1), 
      list(Var = df$Var), mean)




More information about the R-help mailing list