R语言合并Likert量表:将10级因子缩减为5级分类
Recoding 10-Level Likert Scale to 5 Categories in R
Got it, let's walk through how to recode your 10-level Mood_2 factor into 5 meaningful categories. First, let's confirm we're working with the correct ordered levels (since your output shows they're already ordered, which is perfect for Likert scales):
# Check original factor levels levels(dsidata$Mood_2) # Expected output: # [1] "Couldn't be worse" "Very bad" "Bad" "Meh" # [5] "So-so" "Okay" "Good" "Very Good" # [9] "Great" "Really great"
Method 1: Using forcats::fct_collapse (Recommended)
The forcats package (part of the tidyverse) is ideal for this because it preserves the ordered factor structure and keeps your code clean. Here's how to map each pair of original levels to a new category:
# Load the package if you haven't already library(forcats) # Create the new 5-level factor dsidata$Mood_2_5level <- fct_collapse( dsidata$Mood_2, "Very negative" = c("Couldn't be worse", "Very bad"), "Negative" = c("Bad", "Meh"), "Neutral" = c("So-so", "Okay"), "Positive" = c("Good", "Very Good"), "Very positive" = c("Great", "Really great") ) # Verify the result head(dsidata$Mood_2_5level) # Should return: "Very positive" "Very positive" "Negative" "Positive" "Very positive" "Neutral" # Check that the new levels are still ordered correctly levels(dsidata$Mood_2_5level) # [1] "Very negative" "Negative" "Neutral" "Positive" "Very positive"
Method 2: Base R Alternative (recode_factor)
If you prefer sticking to base R (no extra packages), use recode_factor (available in R 4.1 and later). This also maintains the ordered structure:
dsidata$Mood_2_5level <- recode_factor( dsidata$Mood_2, "Couldn't be worse" = "Very negative", "Very bad" = "Very negative", "Bad" = "Negative", "Meh" = "Negative", "So-so" = "Neutral", "Okay" = "Neutral", "Good" = "Positive", "Very Good" = "Positive", "Great" = "Very positive", "Really great" = "Very positive" )
Quick Tips
- Order matters: Since your original factor is ordered, both methods will keep the new categories in logical order (from most negative to most positive)—this is critical for any downstream analysis (like calculating summary stats or running models).
- No missing values: Double-check that every original level is mapped to a new category to avoid unintended
NAs.
内容的提问来源于stack exchange,提问作者Davide Lorino
相关产品推荐
相关产品推荐

