[rkward-devel] (no subject)
Andrés Necochea
yayopoint at gmail.com
Fri Mar 4 15:02:05 UTC 2011
Hi. This is a suggestion for present a bidimensional contingency table
in rkward using an R function.
The function take a xtable as argument and returns a html code to put
in the rk_out.html
This is the function:
html.crosstab <- function(x, calcs="")
{
row.sum <- margin.table(x,1)
row.sum.p <- (row.sum / sum(row.sum)) * 100
row.sum.p <- paste(round(row.sum.p, 2), "%")
col.sum <- margin.table(x,2)
col.sum.p <- (col.sum / sum(col.sum)) * 100
col.sum.p <- paste(round(col.sum.p, 2), "%")
#calcule the props for analysis
if("t" %in% calcs)
tot.p.x <- round(prop.table(x)*100, 2)
if("r" %in% calcs)
row.p.x <- round(prop.table(x, 1)*100, 2)
if("c" %in% calcs)
col.p.x <- round(prop.table(x, 2)*100, 2)
#Begin to write the table
t <- "<table border=1>"
#Header
t <- paste(t, "<tr><td colspan=2>Tabla</td><td colspan=", dim(x)[2],
">", attr(dimnames(x)[2], "names"), "</td><td
rowspan=2>Frecuencia</td><td rowspan=2>Porcentaje</td></tr>", sep="")
t <- paste(t, "<tr><td colspan=2>", attr(dimnames(x)[1], "names"),
"</td>", sep="")
t <- paste(t, paste("<td>", dimnames(x)[[2]], "</td>", sep="",
collapse=""), sep="")
t <- paste(t, "</tr>", sep="")
#Make the rows for each variable
for (i in 1:dim(x)[1])
{
ifelse(calcs[1] != "", rowspanData <- paste(" rowspan=",
length(calcs)+1), rowspanData <- "")
t <- paste(t, "<tr><td", rowspanData,">", dimnames(x)[[1]][i],
"</td><td>Freq</td>", sep="")
t <- paste(t, paste("<td>", x[i,], "</td>", sep="", collapse=""), sep="")
t <- paste(t, "<td", rowspanData, ">", row.sum[i],"</td>", "<td",
rowspanData,">", row.sum.p[i],"</td>")
t <- paste(t, "</tr>", sep="")
if("t" %in% calcs)
t <- paste(t, "<tr><td>Total%</td>", paste("<td>", tot.p.x[i,],"
%</td>", sep="", collapse=""), "</tr>")
if("r" %in% calcs)
t <- paste(t, "<tr><td>Fila%</td>", paste("<td>", row.p.x[i,],"
%</td>", sep="", collapse=""), "</tr>")
if("c" %in% calcs)
t <- paste(t, "<tr><td>Col%</td>", paste("<td>", col.p.x[i,],"
%</td>", sep="", collapse=""), "</tr>")
}
t <- paste(t, "<tr><td colspan=2>Frecuencia</td>", paste("<td>",
col.sum,"</td>", sep="", collapse=""), "<td>", sum(col.sum), "</tr>",
sep="")
t <- paste(t, "<tr><td colspan=2>Porcentaje</td>", paste("<td>",
col.sum.p,"</td>", sep="", collapse=""),"</td></tr>", sep="")
#End of Table
t <- paste(t, "</table>", sep="")
t
}
takes two arguments:
x: a contingency table or crosstabulation (xtable)
calcs: a vector containing the characters c (for column percentage), r
(row%), t (total%) and/or e (chisq expected, not implemented)
here is an example of usage:
# First make the data
gender <- rbinom(100,1,0.5)
gender <- factor(gender, labels=c("male", "female"))
opinion <- rbinom(100,3,0.25)
opinion <- factor(opinion, labels=c("very bad", "bad", "good", "excellent"))
# Now make the crosstab, I prefer xtabs instead of table
tab <- xtabs(~ opinion + gender)
# And now print the table
# The output will include row and column percentage
rk.results(html.crosstab(tab, calcs=c("r", "c"))
I hope that you like this function and wish that could be include in
the N to 1 Crosstabulation plugin. If you like this function I expect
coments or suggestions.
--
Amor y Paz
_ _
\\ //
_ \\//
\\ ^^>
\____/
CHAU
More information about the Rkward-devel
mailing list