Mitzi
Mitzi

Reputation: 57

How do you group factor levels in R?

I have a column in my dataset with likert scale responses that range from Very Likely to Not Likely. I would like to bind a new column that has grouped several of the factor levels. For example, the new column would have only two factor levels: one that consists of Very Likely, Likely and another column with Slightly Likely and Not Likely. I was planning on turning the new column into a numeric and computing some statistical tests. I am hoping to do something like this in the end:

Grouped_Levels <- as.numeric(Grouped_Levels)
mydata <- cbind(mydata, Grouped_Levels) 

Statistical_Testing.aov <- aov(Grouped_Levels ~ Question, data = mydata)

Upvotes: 4

Views: 1231

Answers (1)

jay.sf
jay.sf

Reputation: 72593

Grouping factor levels can easily be done by assigning the grouping in a list. Here an example with toy data:

levels(mydata$value)
# [1] "not likely"      "slightly likely" "likely"          "very likely"    

levels(mydata$value) <- list("unlikely"=c("not likely", "slightly likely"),
                           "likely"=c("likely", "very likely"))
levels(mydata$value)
# [1] "unlikely" "likely"  

After that you probably want to do this:

(Statistical_Testing.aov <- aov(as.integer(value) ~ question, data = mydata))
# Call:
#   aov(formula = as.integer(value) ~ question, data = mydata)
# 
# Terms:
#   question Residuals
# Sum of Squares      0.18      5.82
# Deg. of Freedom        1        23
# 
# Residual standard error: 0.5030343
# Estimated effects may be unbalanced

(Statistical_Testing.anova <- anova(Statistical_Testing.aov))
# Analysis of Variance Table
# 
# Response: as.integer(value)
#           Df Sum Sq Mean Sq F value Pr(>F)
# question   1   0.18 0.18000  0.7113 0.4077
# Residuals 23   5.82 0.25304

Toy data:

set.seed(42)
mydata <- transform(expand.grid(question=1:5, id=1:5),
                    value=factor(sample(1:4, 25, rep=T), 
                                 labels=c("not likely", "slightly likely", 
                                          "likely", "very likely")))

Upvotes: 3

Related Questions