Question

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")
Was it helpful?

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top