Grouping factors in more general ones I have to work on this database on R. I have tried to make a tree model, but all the variables are factors with too many levels. I tried changing the column "operator" in three levels (keeping private, having military for all "military - state" and air company for all the others) using something like getData$operator[grep("Military", getData$operator)] &lt;- "Military" but it changes into NAs since it's not a valid factor.

I could change it to string and make it work, but it doesn't solve the "if not rivate or military, change to air company" part.
Is there any other way?
Make a new column equal to „company“. Use the grep to replace military by „military“, use the grep to replace private by „private“. Change the new column to a factor.