How can I compute group percentages for a categorical variable?

One way to do this is to use rxSummary() to compute the group sums and then calculate the group percentages from these categorical counts.


Here is a simple example that shows how to do this:

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
Note This is a "FAST PUBLISH" article created directly from within the Microsoft support organization. The information contained herein is provided as-is in response to emerging issues. As a result of the speed in making it available, the materials may include typographical errors and may be revised at any time without notice. See Terms of Use for other considerations.

Article ID: 3104216 - Last Review: 10/29/2015 06:25:00 - Revision: 1.0

Revolution Analytics

  • KB3104216