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
[[alternative HTML version deleted]]
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. 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 > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Bert Gunter Genentech Nonclinical Biostatistics 467-7374 http://devo.gene.com/groups/devo/depts/ncb/home.shtml
James,
How about
sprintf('%05d', 210)
It works for fixed length id numbers.
Dave
From:
James Splinter <james.r.splinter@gmail.com>
To:
R-help@r-project.org
Date:
12/21/2010 02:44 PM
Subject:
[R] Keeping Leading Zeros, Treating numbers as text
Sent by:
r-help-bounces@r-project.org
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
[[alternative HTML version deleted]]
______________________________________________
R-help@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.
[[alternative HTML version deleted]]
Use the colClasses argument with a vector of character strings naming the types you want each column to have, and specify "character" for your id column. "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 > > [[alternative HTML version deleted]] > >______________________________________________ >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.--------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity.
Thanks, Works perfectly, don't know why I couldn't find that command! James On Tue, Dec 21, 2010 at 3:56 PM, William Dunlap <wdunlap@tibco.com> wrote:> Use the colClasses= argument to read.csv(). > E.g., > > > txt <- c("City ZipCode Age", "Newport 02840 0", "Seattle 98105 23") > > cat(txt, sep="\n") > City ZipCode Age > Newport 02840 0 > Seattle 98105 23 > > data <- read.csv(textConnection(txt), header=TRUE, > colClasses=c("character","character","numeric"),sep=" ") > > data > City ZipCode Age > 1 Newport 02840 0 > 2 Seattle 98105 23 > > str(data) > 'data.frame': 2 obs. of 3 variables: > $ City : chr "Newport" "Seattle" > $ ZipCode: chr "02840" "98105" > $ Age : num 0 23 > > or > > > data <- read.csv(textConnection(txt), header=TRUE, > colClasses=c("factor","character","numeric"),sep=" ") > > str(data) > 'data.frame': 2 obs. of 3 variables: > $ City : Factor w/ 2 levels "Newport","Seattle": 1 2 > $ ZipCode: chr "02840" "98105" > $ Age : num 0 23 > > Bill Dunlap > Spotfire, TIBCO Software > wdunlap tibco.com > > > -----Original Message----- > > From: r-help-bounces@r-project.org > > [mailto:r-help-bounces@r-project.org] On Behalf Of James Splinter > > Sent: Tuesday, December 21, 2010 12:43 PM > > To: R-help@r-project.org > > Subject: [R] Keeping Leading Zeros, Treating numbers as text > > > > 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 > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help@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. > > >[[alternative HTML version deleted]]