QA: Can I use rxSummary() to bin my data and compute group percentages?

One way to do this would be as follows.

You can compute the total group sums using rxSummary() and use that to compute the group percentages:

df <- data.frame(g = c('1', '0', '1', '0'), a=c(1, 2, 3, 4)) 
sums.within.g <- rxSummary(a ~ g, data = df, summaryStats = "Sum") 
sums.within.g <- sums.within.g$categorical[[1]][,3]

numRows <- length(unique(df$g)) 
totSum <- cumsum(sums.within.g) 
totSum <- totSum[length(totSum)] 
df.percentages <- sums.within.g / totSum 
names(df.percentages) <- levels(df$g)

> df.percentages 
0 1 
0.6 0.4
Properties

Article ID: 3104220 - Last Review: 29 Oct 2015 - Revision: 1

Feedback