Posted By

ascidiacea on 06/24/10


Tagged


Versions (?)

Reorder levels of a factor


 / Published in: R
 

  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  

You need to login to post a comment.