replace multiple values in a column for a single one
You can use function recode()
from library car
to change values also for the factors.
library(car)
a$T<-recode(a$T,"c('E','S','T')='AB'")
If you need to replace different values with different other values then all statements can be written in one function call.
recode(a$T,"c('E','S','T')='AB';c('F','G','H')='CD'")
This would maintain your data structure (a factor like you guessed):
x <- levels(a$T)
levels(a$T) <- ifelse(x %in% c("E","S","T"), "AB", x)
or
levels(a$T)[levels(a$T) %in% c("E","S","T")] <- "AB"
Edit: if you have many such replacements, it is a little more complicated but not impossible:
from <- list(c("E","S","T"), c("J", "K", "L"))
to <- c("AB", "YZ")
find.in.list <- function(x, y) match(TRUE, sapply(y, `%in%`, x = x))
idx.in.list <- sapply(levels(a$T), find.in.list, from)
levels(a$T) <- ifelse(is.na(idx.in.list), levels(a$T), to[idx.in.list])
a$T
# [1] AB F G H I YZ YZ YZ M N O P Q R AB AB
# Levels: AB F G H I YZ M N O P Q R