haven library contains as_factor function that probably does what you want. If not / not quite. Then please consider providing a reprex.