Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Automatically reorder factor levels in dplyr

$
0
0

I have many factor columns, spread randomly throughout a data.frame. These factor columns have 6 levels ranging from "Very Strongly Disagree" to "Very Strongly Agree"

I am looking for a way to automatically re-level every factor variable that has these levels into a pre-specified order. I am currently doing this manually for some 30 columns like so:

data$immigration <- factor(data$immigration,  
    levels = c("Very Strongly Disagree", "Strongly Disagree", "Disagree", "Moderately
    Agree", "Strongly Agree", "Very Strongly Agree"))

Is there an efficient way of doing this in a dplyr chain that automatically relevels ALL the factors which contain the above levels (or really just one of the above levels?)


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>