I have a dataset in .csv format. In my dataset there is one column which is leading with zero like this "05","02". i am trying to import .csv file using read.csv in R. It read successfully but it remove the leading zero. Thanks in Advance.
Asked
Active
Viewed 2.1k times
2
-
5You could specify `colClasses='character' in `read.csv` (not tested though) – akrun Jan 06 '15 at 14:56
-
See also [**how to add leading zeros**](http://stackoverflow.com/questions/5812493/adding-leading-zeros-using-r) when the data is already read. – Henrik Jan 06 '15 at 15:01
1 Answers
1
If all the data in the column are of the same length, you can do paste0("0", NAME)
.
If variable length, try formatC like so: formatC(NAME, width = 2, format = "d", flag = "0")
.
In the latter example, 'd' refers to 'integer' and 'width' can be changed as desired.

meatspace
- 859
- 16
- 25
-
1This solution is good, and works as long as the length of the data is known in advance. That said, if one does not know the length of the data(argument) in advance, it can fail. – Erdogan CEVHER Jun 12 '19 at 06:35