factorize function

Conditionally convert vectors to factors