Search code examples
rdictionaryr-factor

Map levels of factor


How to map the levels of a factor by a reference vector or list?

  a<-factor(sample(1:3,20,T))
  b<-c("1"="B","2"="C","3"="A")
  levels(a)<-list("1"="B","2"="C","3"="A")

Solution

  • something like this???

    > set.seed(1)
    > a<-factor(sample(1:3,20,T))
    > factor(a, levels=c(3,1,2), labels=c("A", "B", "C"))
     [1] B C C A B A A C C B B B A C A C A A C A
    Levels: A B C
    

    See ?factor for further details.