[R] Turning pass/fail results into a proportion

Matthew Walker m.g.walker at massey.ac.nz
Wed May 26 07:07:43 CEST 2004


Please forgive me, I feel exceptionally like a newbie.  Although I've 
read screeds of documentation, I just can't see how this is done.

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) )

I have found the table() function:

table( df$Var, df$Result)

which potentially seems to be part of the solution, however it turns the 
Var column into factors.

As an aside, is the storage of SampleSize (above) the best technique?  
Or is it better to store it in a data frame of its own:

data.frame( Var=c(3,4), SamepleSize=c(3,2) )

and then utilise some sort of "lookup" function?

Thank you for your thoughts,

Matthew Walker




More information about the R-help mailing list