[R] not complete character in csv file

David Winsemius dwinsemius at comcast.net
Mon Dec 12 15:23:02 CET 2011


On Dec 12, 2011, at 7:23 AM, threshold wrote:

> Dear R users, I got the following problem. Given that
>
>> data[3,2]
> [1] "010252"
>
> Code:
> intro <- data.frame()
> intro[1,1] <- as.character(data[3,2])
> write.csv(intro, file='intro.csv')
>
> In 'intro.csv' file I am loosing the 0 in frot of 10252, which I  
> need. Is
> there a way to keep the full character saved? R 2.13.2 (64 bit).

Let me guess. The way you are looking at the output is with Excel and  
you are bothered by the fact that Excel will drop leading zeros from  
items it can interpret as numbers.

If you  use Excel as a viewer, you get what Excel thinks you should  
get. My attempt to format the range where the data will be loaded as  
"Text", and then load from the CSV file, failed to preserve the  
leading "0" using Excel 2011 (for Mac). That strategy used to work for  
me in prior versions of Excel at least for preventinting from  
converting text to dates, but the MS people seem to have decided to be  
even more "helpful".

-- 
David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list