How to replace factor in R

Use levels().

> x <- as.factor(c("A", "B", "C", "D", "B", "A"))
> x
[1] A B C D B A
Levels: A B C D
> levels(x)[levels(x) == "D"] <- "C"
> x
[1] A B C C B A
Levels: A B C

Similar Posts:

Post a Comment

Your email is never published nor shared. Required fields are marked *