The script below illustrate my question:
library(reshape2)
set.seed(1)
dummy.df <- data.frame(var_a=sample(letters[1:5],200,replace=TRUE),
var_b=sample(1:5,200,replace=TRUE),
stringsAsFactors=FALSE)
temp1 <- addmargins(table(dummy.df[,c("var_a","var_b")]),1)
temp2 <- formatC(addmargins(prop.table(table(dummy.df[,c("var_a","var_b")]),2),1)*100,digits=2,format="f")
temp1.melt <- melt(temp1,id.vars="var_a")
temp2.melt <- melt(temp2,id.vars="var_a")
temp.output <- merge(temp1.melt,temp2.melt,by=c("var_a","var_b"))
temp.output[,"value"] <- paste(temp.output[,"value.x"]," (",temp.output[,"value.y"],"%)",sep="")
temp.output[,"var_a"] <- factor(temp.output[,"var_a"],levels=c("a","b","c","d","e","Sum"))
temp.output <- dcast(temp.output,formula=var_a~var_b,value.var="value")
One of my usual work in office is to create tables listing the frequency between different variables, usually I will include the percentage (row/column percentage) in the table also.
Before I know the function addmargins, prop.table and as.data.frame.matrix, I use lots of melt and dcast from reshape2 package to do the trick (i.e. convert the table to dataframe, melt it, do the appropriate division to give the percentage, then dcast it). Now I know using the three new learnt function can save me lots of codes.
Now I wonder if this can be moving one step ahead, without using the script I provided above, and to create a table with row/column percentage present next to the actual count?
If the number of columns is N then this takes the two table and rearranges. Since you have figured out the renaming of columns I will not bore you with that:
(You could use the same matrix transpose trick to choose from two appended vectors of constructed column names.)