How to read numeric values as factors in R?

Ayush Raj Singh picture Ayush Raj Singh · Jun 28, 2013 · Viewed 8.7k times · Source

I have a data frame A which has numeric column like:

zip code
00601
00602
00607

and so on.

If I read this in R using read.csv, they are read as numeric entities. I want them as factors.

I tried converting them back to factor using

A <- as.factor(A)

But this removes starting zeroes and make A like

zip code
601
602
607

I do not want this. I want to save zeroes.

Answer

Hong Ooi picture Hong Ooi · Jun 28, 2013

Use colClasses in your read.csv call to read them in as character or factor: read.csv(*, colClasses="factor").