[R] Keeping Leading Zeros, Treating numbers as text

David Winsemius dwinsemius at comcast.net
Tue Dec 21 22:24:30 CET 2010


On Dec 21, 2010, at 4:11 PM, Bert Gunter wrote:

> Try reading the csv file with, say, Notepad. I think you may find that
> the problem is that Excel assumes the column is numeric and strips off
> the zeros before saving the file. So you need to tell it that the ID
> columns are character before saving.

If Excel turns out to be the culprit, there is an equivalent operation  
to the colClasses specification which you can do to prevent leading  
zeros from being dropped. Select the entire column by clicking on the  
column letter at the top margin of the sheet and then choose Format/ 
Cells/... and pick "Text". The same sort of preparation can also save  
you grief with Date types in Excel or OO.org.


> Then you need to read the Help page for read.csv more carefully,
> noting, in particular, the "colClasses" argument.
>
> -- Bert
>
> On Tue, Dec 21, 2010 at 12:43 PM, James Splinter
> <james.r.splinter at gmail.com> wrote:
>> Hello,
>>
>> I have a data set, with some numerical values, some non-numerical  
>> data, my
>> issue is that I need to preserve my ID numbers (numerics) with the  
>> leading
>> zeros, but when I import the data into R (it's in .csv format)  
>> using the
>> read.csv(" ") command, it turns all the ID numbers (Example: 00210)  
>> into
>> numbers, removing the leading zeros, so I end up with 210. I tried  
>> using the
>> "as.is=" command on the column that I wanted to treat as text, but  
>> it had no
>> effect.
>>
>> Any help would be very much appreciated,
>>
>> Thanks,
>>
>> James
>
>
> -- 
> Bert Gunter
> Genentech Nonclinical Biostatistics
> 467-7374
> http://devo.gene.com/groups/devo/depts/ncb/home.shtml

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list