not complete character in csv file

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

not complete character in csv file

R Kozarski
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).

Thanks, robert

Reply | Threaded
Open this post in threaded view
|

Re: not complete character in csv file

Adams, Jean
threshold wrote on 12/12/2011 06:23:33 AM:

> 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).
>
> Thanks, robert


When I submit similar code (below), the csv file keeps the 0 in front of
the 10252.
        intro <- data.frame()
        intro[1,1] <- "010252"
        write.csv(intro, file='intro.csv')
Have you tried viewing the csv file with a text editor (e.g., Notepad)? If
you view the csv file in spreadsheet software (e.g., Excel) it may format
the values as numbers automatically.

Jean
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: not complete character in csv file

David Winsemius
In reply to this post by R Kozarski

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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: not complete character in csv file

R Kozarski
Indeed in txt it looks fine. Anyway, I must stay without 0 because csv is THE format.

I got another question. why for
Table <- matrix(0,8,3)
day = "Monday"
Table[1,1]=day

all other elements become characters too?

Thanks, robert
Reply | Threaded
Open this post in threaded view
|

Re: not complete character in csv file

Michael Weylandt
Matrix (which is secretly a vector) can only have one mode
(numeric/factor/character/etc.) for all its elements. If you need
multiple types, go to a data frame

Michael


On Mon, Dec 12, 2011 at 10:39 AM, threshold <[hidden email]> wrote:

> Indeed in txt it looks fine. Anyway, I must stay without 0 because csv is THE
> format.
>
> I got another question. why for
> Table <- matrix(0,8,3)
> day = "Monday"
> Table[1,1]=day
>
> all other elements become characters too?
>
> Thanks, robert
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/not-complete-character-in-csv-file-tp4185785p4186427.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: not complete character in csv file

R Kozarski
right,

Table <- data.frame(matrix(0,8,3))
day = "Monday"
Table[1,1]=day
Table[1,2]=3

works, thanks a lot. robert
Reply | Threaded
Open this post in threaded view
|

Re: not complete character in csv file

Rolf Turner-3
In reply to this post by R Kozarski
On 13/12/11 04:39, threshold wrote:
> Indeed in txt it looks fine. Anyway, I must stay without 0 because csv is THE
> format.

It may well be the case that csv is THE format, but csv is not the problem.
Excel is the problem.

The solution is:  Don't use Excel!!!  (This is excellent advice in any  
case.)

     cheers,

         Rolf Turner

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.