Nick Matzke
2010-Sep-10 20:05 UTC
[R] convert "1", "10", and "100" to "0001", "0010", "0100" etc.
Hi, Is there an easy way to convert numbers into a form such that they all have the same number of digits? e.g.: "1", "10", and "100" ...become... "0001", "0010", "0100" etc. I ask because I am producing a large number of files that need to sort consistently by filename. Currently I get this kind of sorting: filename1 filename10 filename11 filename12 filename13 filename14 filename15 filename16 filename17 filename18 filename19 filename2 filename20 filename21 ...etc.. which is annoying. Ideally I'd have: filename0001 filename0002 filename0003 filename0004 filename0005 filename0006 filename0007 filename0008 filename0009 filename0010 filename0011 ...etc.. Basically I want to produce strings like "0010" without an elaborate hack. Thanks! Nick -- ===================================================Nicholas J. Matzke Ph.D. Candidate, Graduate Student Researcher Huelsenbeck Lab Center for Theoretical Evolutionary Genomics 4151 VLSB (Valley Life Sciences Building) Department of Integrative Biology University of California, Berkeley Graduate Student Instructor, IB200A Principles of Phylogenetics: Systematics http://ib.berkeley.edu/courses/ib200a/index.shtml Lab websites: http://ib.berkeley.edu/people/lab_detail.php?lab=54 http://fisher.berkeley.edu/cteg/hlab.html Dept. personal page: http://ib.berkeley.edu/people/students/person_detail.php?person=370 Lab personal page: http://fisher.berkeley.edu/cteg/members/matzke.html Lab phone: 510-643-6299 Dept. fax: 510-643-6264 Cell phone: 510-301-0179 Email: matzke at berkeley.edu Mailing address: Department of Integrative Biology 3060 VLSB #3140 Berkeley, CA 94720-3140 ----------------------------------------------------- "[W]hen people thought the earth was flat, they were wrong. When people thought the earth was spherical, they were wrong. But if you think that thinking the earth is spherical is just as wrong as thinking the earth is flat, then your view is wronger than both of them put together." Isaac Asimov (1989). "The Relativity of Wrong." The Skeptical Inquirer, 14(1), 35-44. Fall 1989. http://chem.tufts.edu/AnswersInScience/RelativityofWrong.htm
David Winsemius
2010-Sep-10 20:10 UTC
[R] convert "1", "10", and "100" to "0001", "0010", "0100" etc.
On Sep 10, 2010, at 4:05 PM, Nick Matzke wrote:> Hi, > > Is there an easy way to convert numbers into a form such that they > all have the same number of digits? > > e.g.: > "1", "10", and "100" > > ...become... > > "0001", "0010", "0100" etc.> sprintf("%05.0f", 100) [1] "00100" > sprintf("%04.0f", 100) [1] "0100"> > I ask because I am producing a large number of files that need to > sort consistently by filename. Currently I get this kind of sorting: > > filename1 > filename10 > filename11 > filename12 > filename13 > filename14 > filename15 > filename16 > filename17 > filename18 > filename19 > filename2 > filename20 > filename21 > ...etc.. > > which is annoying. Ideally I'd have: > > filename0001 > filename0002 > filename0003 > filename0004 > filename0005 > filename0006 > filename0007 > filename0008 > filename0009 > filename0010 > filename0011 > ...etc.. > > Basically I want to produce strings like "0010" without an elaborate > hack. > > Thanks! > Nick > > > > > -- > ===================================================> Nicholas J. Matzke > Ph.D. Candidate, Graduate Student Researcher > Huelsenbeck Lab > Center for Theoretical Evolutionary Genomics > 4151 VLSB (Valley Life Sciences Building) > Department of Integrative Biology > University of California, Berkeley > > Graduate Student Instructor, IB200A > Principles of Phylogenetics: Systematics > http://ib.berkeley.edu/courses/ib200a/index.shtml > > Lab websites: > http://ib.berkeley.edu/people/lab_detail.php?lab=54 > http://fisher.berkeley.edu/cteg/hlab.html > Dept. personal page: http://ib.berkeley.edu/people/students/person_detail.php?person=370 > Lab personal page: http://fisher.berkeley.edu/cteg/members/matzke.html > Lab phone: 510-643-6299 > Dept. fax: 510-643-6264 > Cell phone: 510-301-0179 > Email: matzke at berkeley.edu > > Mailing address: > Department of Integrative Biology > 3060 VLSB #3140 > Berkeley, CA 94720-3140 > > ----------------------------------------------------- > "[W]hen people thought the earth was flat, they were wrong. When > people thought the earth was spherical, they were wrong. But if you > think that thinking the earth is spherical is just as wrong as > thinking the earth is flat, then your view is wronger than both of > them put together." > > Isaac Asimov (1989). "The Relativity of Wrong." The Skeptical > Inquirer, 14(1), 35-44. Fall 1989. > http://chem.tufts.edu/AnswersInScience/RelativityofWrong.htm > > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT
Henrique Dallazuanna
2010-Sep-10 20:14 UTC
[R] convert "1", "10", and "100" to "0001", "0010", "0100" etc.
Try this: formatC(c(1, 10, 100), width = 4, flag = '0') On Fri, Sep 10, 2010 at 5:05 PM, Nick Matzke <matzke@berkeley.edu> wrote:> Hi, > > Is there an easy way to convert numbers into a form such that they all have > the same number of digits? > > e.g.: > "1", "10", and "100" > > ...become... > > "0001", "0010", "0100" etc. > > I ask because I am producing a large number of files that need to sort > consistently by filename. Currently I get this kind of sorting: > > filename1 > filename10 > filename11 > filename12 > filename13 > filename14 > filename15 > filename16 > filename17 > filename18 > filename19 > filename2 > filename20 > filename21 > ...etc.. > > which is annoying. Ideally I'd have: > > filename0001 > filename0002 > filename0003 > filename0004 > filename0005 > filename0006 > filename0007 > filename0008 > filename0009 > filename0010 > filename0011 > ...etc.. > > Basically I want to produce strings like "0010" without an elaborate hack. > > Thanks! > Nick > > > > > -- > ===================================================> Nicholas J. Matzke > Ph.D. Candidate, Graduate Student Researcher > Huelsenbeck Lab > Center for Theoretical Evolutionary Genomics > 4151 VLSB (Valley Life Sciences Building) > Department of Integrative Biology > University of California, Berkeley > > Graduate Student Instructor, IB200A > Principles of Phylogenetics: Systematics > http://ib.berkeley.edu/courses/ib200a/index.shtml > > Lab websites: > http://ib.berkeley.edu/people/lab_detail.php?lab=54 > http://fisher.berkeley.edu/cteg/hlab.html > Dept. personal page: > http://ib.berkeley.edu/people/students/person_detail.php?person=370 > Lab personal page: http://fisher.berkeley.edu/cteg/members/matzke.html > Lab phone: 510-643-6299 > Dept. fax: 510-643-6264 > Cell phone: 510-301-0179 > Email: matzke@berkeley.edu > > Mailing address: > Department of Integrative Biology > 3060 VLSB #3140 > Berkeley, CA 94720-3140 > > ----------------------------------------------------- > "[W]hen people thought the earth was flat, they were wrong. When people > thought the earth was spherical, they were wrong. But if you think that > thinking the earth is spherical is just as wrong as thinking the earth is > flat, then your view is wronger than both of them put together." > > Isaac Asimov (1989). "The Relativity of Wrong." The Skeptical Inquirer, > 14(1), 35-44. Fall 1989. > http://chem.tufts.edu/AnswersInScience/RelativityofWrong.htm > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Peng, C
2010-Sep-11 01:25 UTC
[R] convert "1", "10", and "100" to "0001", "0010", "0100" etc.
These are character values. Is there any way to get 001, 010, ..., as actual numeric values? -- View this message in context: http://r.789695.n4.nabble.com/convert-1-10-and-100-to-0001-0010-0100-etc-tp2535023p2535296.html Sent from the R help mailing list archive at Nabble.com.
David Winsemius
2010-Sep-11 01:55 UTC
[R] convert "1", "10", and "100" to "0001", "0010", "0100" etc.
On Sep 10, 2010, at 9:25 PM, Peng, C wrote:> > These are character values. Is there any way to get 001, 010, ..., > as actual > numeric values?> 001 [1] 1 > 0100 [1] 100 > 001 == 1 [1] TRUE > 0100 == 100 [1] TRUE> -- > View this message in context: http://r.789695.n4.nabble.com/convert-1-10-and-100-to-0001-0010-0100-etc-tp2535023p2535296.html > Sent from the R help mailing list archive at Nabble.com.-- David Winsemius, MD West Hartford, CT
Peng, C
2010-Sep-11 02:05 UTC
[R] convert "1", "10", and "100" to "0001", "0010", "0100" etc.
I mean to display 001,010, ..., as there are. In other words, whether there is a function, say func(), such that func(001,010) displays 001, 010. -- View this message in context: http://r.789695.n4.nabble.com/convert-1-10-and-100-to-0001-0010-0100-etc-tp2535023p2535318.html Sent from the R help mailing list archive at Nabble.com.
David Winsemius
2010-Sep-11 02:36 UTC
[R] convert "1", "10", and "100" to "0001", "0010", "0100" etc.
On Sep 10, 2010, at 10:05 PM, Peng, C wrote:> > I mean to display 001,010, ..., as there are. In other words, > whether there > is a function, say func(), such that func(001,010) displays 001, 010.Not hard to construct one, but does not behave properly in the sub- unity decimal range. Not surprising. It did better in the range of values from the integer class than I thought it would. > func4 <- function(x) cat(sprintf("%05.0f", x)) > func4(10) 00010 > func5 <- function(x) cat(sprintf("%05.0f", x)) > func5(100) 00100 > func5(100000) 100000 > func5(100000000) 100000000 > func5(0.001) 00000 > func5(-3) -0003 > func5(-3000000) -3000000 -- David.
Peng, C
2010-Sep-11 03:00 UTC
[R] convert "1", "10", and "100" to "0001", "0010", "0100" etc.
Thanks David. func() simply prints out the 0010 as a text value. It is still not numeric. I am just curious about it.> is.numeric(func4(0100))00100[1] FALSE -- View this message in context: http://r.789695.n4.nabble.com/convert-1-10-and-100-to-0001-0010-0100-etc-tp2535023p2535345.html Sent from the R help mailing list archive at Nabble.com.
Peter Dalgaard
2010-Sep-11 08:55 UTC
[R] convert "1", "10", and "100" to "0001", "0010", "0100" etc.
On 09/11/2010 05:00 AM, Peng, C wrote:> > Thanks David. > func() simply prints out the 0010 as a text value. It is still not numeric. > I am just curious about it. > >> is.numeric(func4(0100)) > 00100[1] FALSE > >Well, you can look at the kind of things as.octmode & friends do. The only thing you can do is to change the print method; internally there can be no difference between the representation of 10 and 010. -- Peter Dalgaard Center for Statistics, Copenhagen Business School Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com