Steve
Steve

Reputation: 13

Categorical Variables Table with Percentages in R

I have a series of categorical variables that have the response options (Favorable, Unfavorable, Neutral).

I want to create a table in R that will give the list of all 10 variables in rows (one variable per row) - with the percentage response "Favorable, Unfavorable, Neutral" in the columns. Is this possible in R? Ideally, I would also want to be able to group this by another categorical variable (e.g. to compare how males vs. females responded to the questions differently).

Upvotes: 0

Views: 3880

Answers (2)

lefft
lefft

Reputation: 2105

You'll get better answers if you provide a sample of your actual data (see this post). That said, here is a solution using dplyr:: (and reshape2::melt).

# function to create a column of fake data 
make_var <- function(n=100) sample(c("good","bad","ugly"), size=n, replace=TRUE)

# put ten of them together 
dat <- as.data.frame(replicate(10, make_var()), stringsAsFactors=FALSE) 

library("dplyr")

# then reshape to long format, group, and summarize -- 
dat %>% reshape2::melt(NULL) %>% group_by(variable) %>% summarize(
    good_pct = (sum(value=="good") / length(value)) * 100, 
    bad_pct = (sum(value=="bad") / length(value)) * 100, 
    ugly_pct = (sum(value=="ugly") / length(value)) * 100
)

Note that to group by another column (e.g. sex), you can just say group_by(variable, sex) before you summarize (as long as sex is a column of the data, which isn't the case in this constructed example).

Upvotes: 2

Marius
Marius

Reputation: 60070

Adapting lefft's example but trying to do everything in dplyr:

dat %>% 
    gather(variable, value) %>%
    group_by(variable) %>%
    count(value) %>%
    mutate(pct = n / sum(n) * 100) %>%
    select(-n) %>%
    spread(value, pct)

Upvotes: -1

Related Questions