Search code examples
rcsvr-factor

How to read numeric values as factors in R?


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.


Solution

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