Hello All,

Am learning to create tables with n(%) formatting using R. Below is a working 
example. I think this is not bad but wondered if there are better ways of doing 
it. Although it can be quite humbling, seeing good code helps me assess my 
progress as an R programmer. 

Ultimately want to have code that I can turn into a function. Will then use the 
output produced to make tables using odfWeave and Sweave/knitr.

Thanks,

Paul

breaks <- as.data.frame(lapply(warpbreaks, function(x) rep(x, 
warpbreaks$breaks)))

Freq <- with(breaks, addmargins(table(wool, tension), 2))
Prop <- round( prop.table(Freq, 2) * 100, 2 )
Freq <- addmargins(Freq, 1)
Prop <- addmargins(Prop, 1)

require(odfWeave)
class(Freq) <- "character"
class(Prop) <- "character"
FreqProp <- matrixPaste(Freq, "(", Prop, "%)", sep = c("", "", ""))
colnames(FreqProp) <- colnames(Freq)
rownames(FreqProp) <- rownames(Freq)
names(dimnames(FreqProp)) <- c("Wool", "")

FreqProp <- data.frame(Wool=rownames(FreqProp), FreqProp, row.names=NULL, 
stringsAsFactors=FALSE)
names(FreqProp)[names(FreqProp) == "Sum"] <- "Total"
FreqProp$Wool[FreqProp$Wool == "Sum"] <- "Total"
FreqProp

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to