We can borrow from existing summary routines and make it a little less invasive, giving the factors a transition attribute of an additional class.
summary.my.factor<-function(object,...) { x<-prop.table(table(object)) setNames(sprintf("%1.2f%%",100*x),names(x)) } my.summary<-function(object,...) { f<-function(x) if(inherits(x,"factor")) structure(x,class=c("my.factor",class(x))) else x summary(as.data.frame(lapply(object,f)),...) } my.summary(chickwts)
weight feed
Min. : 108.0 casein: 16.90%
1st Qu.:204.5 horsebean: 14.08%
Median: 258.0 linseed: 16.90%
Mean: 261.3 meatmeal: 15.49%
3rd Qu.:323.5 soybean: 19.72%
Max : 423.0 sunflower: 16.90%
I did not bother to respect any options, such as digits in my.factor formatting.
source share