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

2

7

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: enter image description here

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

Toothbrush answered 14/5, 2013 at 9:48 Comment(0)
C
5

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")
Confess answered 17/5, 2013 at 14:31 Comment(0)
B
0

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
Briarwood answered 6/7, 2014 at 21:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.