Reorder levels of a factor


/ Published in: R
Save to your folder(s)



Copy this code and paste it in your HTML
  1. # x is already a factor
  2. levels(x)
  3. # [1] "Large" "Medium" "Small"
  4.  
  5. # various ways to reorder levels to be Small, Medium, Large
  6. factor(x, levels = levels(x)[3:1])
  7. factor(x, levels = c("Small", "Medium", "Large"))
  8.  
  9. # x is not yet a factor (e.g., a character vector or data.frame col)
  10. factor(x, levels = levels(factor(x))[3:1])
  11. factor(x, levels = c("Small", "Medium", "Large"))

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.