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 -- View this message in context: http://r.789695.n4.nabble.com/not-complete-character-in-csv-file-tp4185785p4185785.html Sent from the R help mailing list archive at Nabble.com.
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, robertWhen 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]]
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
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.
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 <r.kozarski at gmail.com> 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. > > ______________________________________________ > R-help at r-project.org 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.
right, Table <- data.frame(matrix(0,8,3)) day = "Monday" Table[1,1]=day Table[1,2]=3 works, thanks a lot. robert -- View this message in context: http://r.789695.n4.nabble.com/not-complete-character-in-csv-file-tp4185785p4186639.html Sent from the R help mailing list archive at Nabble.com.