Row Percentages in crosstable generated from summary() from the Hmisc package

I have been trying to learn to use the summary()-function from the Hmisc-package to generate crosstables that include chisquared tests. With help from this board I'm almost there. I just can't figure out how to obtain row-percentages instead of column percentages.

#Data:
v1 <- sample(letters[8:12],200,replace=TRUE)
v2 <- sample(letters[1:2],200,replace=TRUE)
month <- sample(month.name[7:9],200,replace=TRUE)
df <- data.frame(v1,v2,month)

#Table:
latex(    summary( month  ~ v1 + v2 , data=df,  method="reverse" ,test=TRUE),        exclude1=FALSE,file="",booktabs=TRUE,long=TRUE)

Which gets me this:

This gets me the column-percentages. I am looking for a way to turn it around so i get the row-percentages instead. I've been searching the Hmisc-documentation for "row" and "column" and "percent" but no luck. The summary.formular() function has the optional argument "fun" but it is over my head to get it to do row percentages...

Please Help

Answers


If you hack around a bit with the Hmisc::formatCats. Namely, change the MARGIN from 2 to 1. You can get there.

Part of formatCats

denom <- if (type == 1) apply(tab, 2, sum) else group.freq
pct <- 100 * (if (ncol(tab) > 1) sweep(tab, 2, denom, FUN = "/") else tab/denom)

Change to

denom <- if (type == 1) apply(tab, 1, sum) else group.freq
pct <- 100 * (if (ncol(tab) > 1) sweep(tab, 1, denom, FUN = "/") else tab/denom)

I made a gist at https://gist.github.com/jwijffels/5599349 with this modified function called myformatCats. Get it, assign it in the Hmisc namespace to override Hmisc::formatCats and it prints out the col pct.

require(Hmisc)
require(devtools)
source_gist("5599349")
assignInNamespace(x="formatCats", value=myformatCats, ns="Hmisc")

v1 <- sample(letters[8:12],200,replace=TRUE)
v2 <- sample(letters[1:2],200,replace=TRUE)
month <- sample(month.name[7:9],200,replace=TRUE)
df <- data.frame(v1,v2,month)
summary( month  ~ v1 + v2 , data=df,  method="reverse")

For some reason I could not add a comment. I tried the solution by jwijjfels but it didnĀ“t work. Turns out Hmisc has changed so in Hmisc 3.14-3 you have to make the following changes:

Edit line 15-21 in Hmisc:::formatCats into the following and replace this function with formatCats as described by jwijffels.

denom <- if (type == 1) 
    apply(tab, 1, sum)
  else group.freq
pct <- if (ncol(tab) > 1) 
   sweep(tab, 1, denom, FUN = "/")
  else tab/denom

Need Your Help

Can I, and should I, force the contents of DGV cells to be null?

c# winforms generics datagridview

I'm saving values entered in a DGV to a List&lt;T>; they are decimal values or nothing (empty). If empty, I want them to be viewed as null, so that my helper function will see those empty vals as 0...

Implement dependency check with command line params in NSIS scripts

installation dependencies command-line-arguments nsis silent

My goal is to know if you use any check or code snipp to determine if ther is some dependecy when passing NSIS command line params to copiled setup using Silent installation ( /S param ).

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.