outis
outis

Reputation: 831

How to manipulate summary table results

I have some data that looks like this:

          A      B
6     Often  Often
7    Always Always
8    Rarely Rarely
9 Sometimes  Often

structure(list(A = structure(c(5L, 6L, 3L, 4L), .Label = c("", 
"Almost Never", "Rarely", "Sometimes", "Often", "Always"), class = c("ordered", 
"factor")), B = structure(c(5L, 6L, 3L, 5L), .Label = c("", "Almost Never", 
"Rarely", "Sometimes", "Often", "Always"), class = c("ordered", 
"factor"))), .Names = c("A", "B"), row.names = 6:9, class = "data.frame")

Using summary, I get the counts of each type of response against the possible responses, which is exactly what I want:

            A                B    
             :0               :0  
 Almost Never:0   Almost Never:0  
 Rarely      :1   Rarely      :1  
 Sometimes   :1   Sometimes   :0  
 Often       :1   Often       :2  
 Always      :1   Always      :1  

Now I want to manipulate these numbers to get (Often + Always)/Total responses. The summary output is character output, though -- I can split on the colon, but there must be a better way.

How can I calculate the percentage of Often + Total responses per question given the data set above?

Upvotes: 1

Views: 189

Answers (1)

David Robinson
David Robinson

Reputation: 78600

This can be done using apply and table (assuming d is your data frame):

apply(d, 2, function(col) {
    tab = table(col)
    (tab["Often"] + tab["Always"]) / sum(tab)
})

Note that the above will work only if there is always at least "Always" and one "Often" in each column. The following is slightly less concise but will work even if "Always" or "Often" is missing from a column:

sapply(1:NCOL(d), function(i) {
        tab = table(d[, i])
        (tab["Often"] + tab["Always"]) / sum(tab)
})

Upvotes: 1

Related Questions