Fixed a bug in formula handling

Correctly replace as.factor string