[R] Formatting numbers

baptiste auguie ba208 at exeter.ac.uk
Mon Apr 27 12:47:57 CEST 2009


sprintf("%04.f", 2)

sapply(sample(1:1000,4), function(ii) sprintf("%04.f",ii))

?sprintf


HTH,

baptiste

On 27 Apr 2009, at 11:35, Mario dos Reis wrote:

> I've been trough the R documentation for about half an hour and it's  
> not
> clear to me how to do this:
>
> I need to format to character a series of integers from 1 to 1000,  
> and I
> like them to look like
>
> "0001" "0002", "0059", "0123" and so on. Padded with zeroes to have  
> four
> digits.
>
> Cheers!
> Mario.
>
> r-help-request at r-project.org wrote:
>> Send R-help mailing list submissions to
>>      r-help at r-project.org
>>
>> To subscribe or unsubscribe via the World Wide Web, visit
>>      https://stat.ethz.ch/mailman/listinfo/r-help
>> or, via email, send a message with subject or body 'help' to
>>      r-help-request at r-project.org
>>
>> You can reach the person managing the list at
>>      r-help-owner at r-project.org
>>
>> When replying, please edit your Subject line so it is more specific
>> than "Re: Contents of R-help digest..."
>>
>>
>> Today's Topics:
>>
>>   1. Re: Changing gird marks in ggplot2 (ONKELINX, Thierry)
>>   2. Re: argument 'exclude' in function xtabs (Matthieu Lesnoff)
>>   3.  Question of "Quantile Regression for Longitudinal Data"
>>      (Helen Chen)
>>   4. Conditional plot labels (Christian Bustamante)
>>   5.  simulate arima model (Rebecca1117)
>>   6.  THE EQUIVALENT OF SQL INNER TABLE JOIN IN R (Nigel Birney)
>>   7. Scatterplot of two groups side-by-side? (nonunah at yahoo.de)
>>   8. Re: Nomogram with stratified cph in Design package (reneepark)
>>   9.  Stochastic Gradient Ascent for logistic regression (Tim LIU)
>>  10. Re: Question of "Quantile Regression for Longitudinal Data"
>>      (Tirthankar Chakravarty)
>>  11. Re: THE EQUIVALENT OF SQL INNER TABLE JOIN IN R
>>      (Gabor Grothendieck)
>>  12. Re: THE EQUIVALENT OF SQL INNER TABLE JOIN IN R (Peter Dalgaard)
>>  13. Re: 3 questions regarding matrix copy/shuffle/compares (Esmail)
>>  14. Re: Conditional plot labels (baptiste auguie)
>>  15. Re: Scatterplot of two groups side-by-side? (baptiste auguie)
>>  16. Help to select the raw in a data.frame with the max value
>>      (Alessandro)
>>  17. re moving entries from one vector that are in another  
>> (onyourmark)
>>  18. Is their any function can generate orthogonal tables(e.g.
>>      L_8(2^7) (dzuswxbylw)
>>  19. Re: dotplot: labeling coordinates for each point (Qifei Zhu)
>>  20. Re: Help to select the raw in a data.frame with the max value
>>      (Jorge Ivan Velez)
>>  21. Re: Nomogram with stratified cph in Design package
>>      (Frank E Harrell Jr)
>>  22. Problem installing packages (Jarek Jasiewicz)
>>  23. Re: 3 questions regarding matrix copy/shuffle/compares
>>      (David Winsemius)
>>  24. Re: issue building my own package... moving from Apple OS to
>>      Windows (Uwe Ligges)
>>  25. Re: Scatterplot of two groups side-by-side? (Stefan Grosse)
>>  26. Re: 3 questions regarding matrix copy/shuffle/compares (Esmail)
>>  27. R:  constrained optimization (mauede at alice.it)
>>  28. Re: Help to select the raw in a data.frame with the max value
>>      (David Winsemius)
>>  29. Re: Scatterplot of two groups side-by-side? (John Fox)
>>  30. Re: Problem installing packages (Uwe Ligges)
>>  31. Re: fclustindex, e1071 package (Uwe Ligges)
>>  32. Re: Overlapping parameters "k" in different functions in
>>      "ipred" (Uwe Ligges)
>>  33. Re: How to get rid of loop? (Uwe Ligges)
>>  34. Re: help with plotting results of lda (Uwe Ligges)
>>  35. Re: Problem installing packages (Jarek Jasiewicz)
>>  36. Re: 3 questions regarding matrix copy/shuffle/compares
>>      (David Winsemius)
>>  37. Re: Problem installing packages (Jarek Jasiewicz)
>>  38. Memory issues in R (Neotropical bat risk assessments)
>>  39. Re: 3 questions regarding matrix copy/shuffle/compares
>>      (hadley wickham)
>>  40. Re: 3 questions regarding matrix copy/shuffle/compares
>>      (David Winsemius)
>>  41. Re: 3 questions regarding matrix copy/shuffle/compares (Esmail)
>>  42. Re: 3 questions regarding matrix copy/shuffle/compares
>>      (hadley wickham)
>>  43. Re: 3 questions regarding matrix copy/shuffle/compares (Esmail)
>>  44. Re: 3 questions regarding matrix copy/shuffle/compares (Esmail)
>>  45. Re: Stochastic Gradient Ascent for logistic regression
>>      (Ravi Varadhan)
>>  46. Re: Problem installing packages (Uwe Ligges)
>>  47. Re: Memory issues in R (David Winsemius)
>>  48. Re: Memory issues in R (Stefan Grosse)
>>  49. Matching in R (dirk567 at gmx.de)
>>  50. Re: help with plotting results of lda (Prof Brian Ripley)
>>  51. Install packages not working in latest version?
>>      (Neotropical bat risk assessments)
>>  52. Re: Install packages not working in latest version? (Uwe Ligges)
>>  53. comparing matrices (Esmail)
>>  54. doubt in vglm output (priyabrata panigrahi)
>>  55.  figure layout (hesicaia)
>>  56. Re: ANOVA/statistics question (drmh)
>>  57. Re: comparing matrices (ONKELINX, Thierry)
>>  58.  Problem with create a tree (Grze?)
>>  59. Re: THE EQUIVALENT OF SQL INNER TABLE JOIN IN R
>>      (Wacek Kusnierczyk)
>>  60. Re: Memory issues in R (David Winsemius)
>>  61. Re: comparing matrices (baptiste auguie)
>>  62. Re: Problem installing packages (Jarek Jasiewicz)
>>  63. Re: comparing matrices (David Winsemius)
>>  64. Re: comparing matrices (Esmail)
>>  65. Re: comparing matrices (Esmail)
>>  66. Re: comparing matrices (Esmail)
>>  67. Re: figure layout (Cuvelier Etienne)
>>  68. eager to learn how to use "sapply", "lapply", ...
>>      (mauede at alice.it)
>>  69. Re: eager to learn how to use "sapply", "lapply", ...
>>      (jim holtman)
>>  70. Re: ANOVA/statistics question (Peter Flom)
>>  71. Re: eager to learn how to use "sapply", "lapply", ...
>>      (Gabor Grothendieck)
>>  72. Re: dotplot: labeling coordinates for each point (Deepayan  
>> Sarkar)
>>  73. Generate ramified structures (Talita Perciano)
>>  74. Rmpi failing to install with all latest MPI packages and
>>      config  arguments (Vince Fulco)
>>  75. RWeka: How to access AttributeEvaluators (Michael Olschimke)
>>  76. Re: simulate arima model (Rolf Turner)
>>  77. Re: Question of "Quantile Regression for Longitudinal Data"
>>      (roger koenker)
>>  78. lme -  nlminb problem, convergence error code = 1 (Katrin Wolf)
>>  79.  R 64-bit for Ubuntu 9.04 64-bit (Tom La Bone)
>>  80. how to install R really *locally*? (Oliver Kullmann)
>>  81. Re: How to get rid of loop? (Peter Alspach)
>>  82. Re: Rmpi failing to install with all latest MPI packages and
>>      config  arguments (Dirk Eddelbuettel)
>>  83. Re: R 64-bit for Ubuntu 9.04 64-bit (Dirk Eddelbuettel)
>>  84. Re: Rmpi failing to install with all latest MPI packages and
>>      config arguments (Vince Fulco)
>>  85. Re: function returns R object with name based on input
>>      (Jennifer Brea)
>>  86.  Bumps chart in R (Andreas Christoffersen)
>>  87. Re: eager to learn how to use "sapply", "lapply", ...
>>      (hadley wickham)
>>  88. Re: Bumps chart in R (hadley wickham)
>>  89. Re: Bumps chart in R (Gabor Grothendieck)
>>  90. RODBC - XLSX files - dropping/clearing sheets (Daniel Bradley)
>>  91. Re: How to get rid of loop? (Ken-JP)
>>  92. Re: Bumps chart in R (Mike Lawrence)
>>  93. How to create a graph layout? (Christian Bustamante)
>>  94. Plotting polynomial fit (Ronnen Levinson)
>>  95. Re: RODBC - XLSX files - dropping/clearing sheets
>>      (Gabor Grothendieck)
>>  96. Re: How to create a graph layout? (Sarah Goslee)
>>  97. Re: how to inspect content of save() file (Benno P?tz)
>>  98.  Flipping axes of qqnorm (Chris_d)
>>  99.  Unable to re-import a table that was just exported to a file
>>      (Nigel Birney)
>>  100.  Cannot clean infinite values (Nigel Birney)
>>  101. Re: Flipping axes of qqnorm (Sundar Dorai-Raj)
>>  102. Re: Cannot clean infinite values (Sundar Dorai-Raj)
>>  103. Re: Cannot clean infinite values (Charles Annis, P.E.)
>>  104. Re: Flipping axes of qqnorm (Charles Annis, P.E.)
>>  105. Re: Cannot clean infinite values (Ben Bolker)
>>  106. Re: Plotting polynomial fit (David Winsemius)
>>  107. Re: Flipping axes of qqnorm (Bill.Venables at csiro.au)
>>  108. Re: Memory issues in R (Ben Bolker)
>>  109. Re: prediction intervals (alpha and beta) for model average
>>      estimates from binomial glm and model.avg (library=dRedging)
>>      (Michelle Ensbey)
>>  110. [R-pkgs] New package: CHNOSZ (Jeffrey Dick)
>>  111. Sub-saharan Africa R user forum (justin bem)
>>  112. Re: How to create a graph layout? (Cuvelier Etienne)
>>  113. MDL - Fayyad & Irani Discretization (Piotr Roma?ski)
>>  114. Re: how to install R really *locally*? (Uwe Ligges)
>>  115. Re: How to get rid of loop? (Uwe Ligges)
>>  116. Re: Bumps chart in R (Andreas Christoffersen)
>>  117. Re: Bumps chart in R (Andreas Christoffersen)
>>  118. Re: Bumps chart in R (Andreas Christoffersen)
>>  119. Re: MDL - Fayyad & Irani Discretization (Hans W. Borchers)
>>  120. Re: Scatterplot of two groups side-by-side? (Jim Lemon)
>>  121. Passing Bablok (botto)
>>
>>
>> ----------------------------------------------------------------------
>>
>> Message: 1
>> Date: Sun, 26 Apr 2009 12:03:54 +0200
>> From: "ONKELINX, Thierry" <Thierry.ONKELINX at inbo.be>
>> Subject: Re: [R] Changing gird marks in ggplot2
>> To: "Christopher Desjardins" <cddesjardins at gmail.com>,
>>      <r-help at r-project.org>
>> Message-ID:
>>      <2E9C414912813E4EB981326983E0A10406653ACE at inboexch.inbo.be>
>> Content-Type: text/plain;     charset="us-ascii"
>>
>> Dear Chris,
>>
>> Changing
>>
>>      coord_cartesian(ylim = c(0, 5))
>>
>> into
>>
>>      coord_cartesian() + scale_y_continuous(limits = c(0, 5))
>>
>> That should solve your problem.
>>
>> HTH,
>>
>> Thierry
>>
>> ------------------------------------------------------------------------
>> ----
>> ir. Thierry Onkelinx
>> Instituut voor natuur- en bosonderzoek / Research Institute for  
>> Nature
>> and Forest
>> Cel biometrie, methodologie en kwaliteitszorg / Section biometrics,
>> methodology and quality assurance
>> Gaverstraat 4
>> 9500 Geraardsbergen
>> Belgium
>> tel. + 32 54/436 185
>> Thierry.Onkelinx at inbo.be
>> www.inbo.be
>>
>> To call in the statistician after the experiment is done may be no  
>> more
>> than asking him to perform a post-mortem examination: he may be  
>> able to
>> say what the experiment died of.
>> ~ Sir Ronald Aylmer Fisher
>>
>> The plural of anecdote is not data.
>> ~ Roger Brinner
>>
>> The combination of some data and an aching desire for an answer  
>> does not
>> ensure that a reasonable answer can be extracted from a given body of
>> data.
>> ~ John Tukey
>>
>> -----Oorspronkelijk bericht-----
>> Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org 
>> ]
>> Namens Christopher Desjardins
>> Verzonden: zaterdag 25 april 2009 6:52
>> Aan: r-help at r-project.org
>> Onderwerp: [R] Changing gird marks in ggplot2
>>
>> Hi,
>> When I zoom into a graph created in ggplot2 with the
>> coord_cartesian(ylim=c(0,5)) option, I have no values labelled on my
>> y-axis.
>> For this graph ggplot2 only puts labels the y-axis at intervals of 10
>> (i.e.
>> 0, 10, 20, ...). However, the major portion of the graph I am  
>> interested
>> in
>> is located between the values of 0 and 5 on the y-axis (thus why I am
>> zoooming). How can I coerce ggplot2 into making the major gird  
>> marks so
>> that
>> 0, 1, 2, 3, 4, and 5 are shown as it is currently showing no label?
>> Thanks. Also please cc me directly as I'm a digest subscriber.
>> Chris
>>
>>      [[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.
>>
>> Dit bericht en eventuele bijlagen geven enkel de visie van de  
>> schrijver weer
>> en binden het INBO onder geen enkel beding, zolang dit bericht niet  
>> bevestigd is
>> door een geldig ondertekend document. The views expressed in  this  
>> message
>> and any annex are purely those of the writer and may not be  
>> regarded as stating
>> an official position of INBO, as long as the message is not  
>> confirmed by a duly
>> signed document.
>>
>>
>>
>> ------------------------------
>>
>> Message: 2
>> Date: Sun, 26 Apr 2009 12:45:29 +0200
>> From: Matthieu Lesnoff <matthieu.lesnoff at gmail.com>
>> Subject: Re: [R] argument 'exclude' in function xtabs
>> To: iverson at biostat.wisc.edu
>> Cc: r-help at r-project.org
>> Message-ID:
>>      <2d0450630904260345j44ebc947pf9a80e25a87713d2 at mail.gmail.com>
>> Content-Type: text/plain
>>
>> Ok thanks Erik
>>
>> Indeed, "exclude" in "xtabs" seems working whith character vectors:
>>
>>
>>> x <- c(rep(c("A","B","C"), 2))
>>> x
>>>
>> [1] "A" "B" "C" "A" "B" "C"
>>
>>> xtabs(~ x, exclude = "B")
>>>
>> x
>> A C
>> 2 2
>>
>> and not directly with factors:
>>
>>
>>> x <- factor(rep(c("A","B","C"), 2))
>>> x
>>>
>> [1] A B C A B C
>> Levels: A B C
>>
>>> levels(x)
>>>
>> [1] "A" "B" "C"
>>
>>> xtabs(~ x, exclude = "B")
>>>
>> x
>> A B C
>> 2 2 2
>>
>> while function "table" does it:
>>
>>
>>> x <- factor(rep(c("A","B","C"), 2))
>>> x
>>>
>> [1] A B C A B C
>> Levels: A B C
>>
>>> table(x, exclude = "B")
>>>
>> x
>> A C
>> 2 2
>>
>> However, one point remains confusing for me in function "table".  
>> When I add
>> a NA in the vector, it still works:
>>
>>
>>> x <- factor(c(rep(c("A","B","C"), 2), NA))
>>> x
>>>
>> [1] A    B    C    A    B    C    <NA>
>> Levels: A B C
>>
>>> table(x, exclude = "B")
>>>
>> x
>> A C
>> 2 2
>>
>> but not anymore when I use argument "exclude = NULL" in function  
>> "factor":
>>
>>
>>> x <- factor(c(rep(c("A","B","C"), 2), NA), exclude = NULL)
>>> x
>>>
>> [1] A    B    C    A    B    C    <NA>
>> Levels: A B C <NA>
>>
>>> table(x, exclude = "B")
>>>
>> x
>>  A    B    C <NA>
>>  2    2    2    1
>>
>> Finally, if I remove NA, it works again:
>>
>>
>>> x <- factor(rep(c("A","B","C"), 2), exclude = NULL)
>>> x
>>>
>> [1] A B C A B C
>> Levels: A B C
>>
>>> table(x, exclude = "B")
>>>
>> x
>> A C
>> 2 2
>>
>> it looks like "exclude" does not work when NA is a level
>>
>>
>>
>>
>> Regards
>>
>> Matthieu
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>> -----Message d'origine-----
>>> De : Erik Iverson [mailto:iverson at biostat.wisc.edu]
>>> Envoyé : 24 April 2009 19:13
>>> À : Matthieu Lesnoff
>>> Cc : r-help at r-project.org
>>> Objet : Re: [R] argument 'exclude' in function xtabs
>>>
>>>
>>>
>>>> I was willing to use argument 'exclude' in function xtabs to remove
>>>> some levels of factors (xtabs help page says '"exclude: a vector of
>>>> values to be excluded when forming the set of levels of the
>>>>
>>> classifying factors").
>>>
>>>
>>> I think I see what's happening, and it's a little confusing
>>> to me, too.
>>> If your classifying factor to xtabs is not actually a factor,
>>> but say, a character vector, it will be converted to a factor
>>> while using the "exclude" argument.  However, if it already
>>> is a factor, this does not happen.
>>> So, for example, contrast:
>>>
>>> trt.char <- c("A","B","C","A","B","C")
>>> xtabs(~trt.char, exclude = "B")
>>>
>>> with
>>>
>>> trt.fac <- factor(c("A","B","C","A","B","C"))
>>> xtabs(~trt.fac, exclude = "B")
>>>
>>> Hope that helps,
>>> Erik Iverson
>>>
>>>
>>
>>      [[alternative HTML version deleted]]
>>
>>
>>
>> ------------------------------
>>
>> Message: 3
>> Date: Sun, 26 Apr 2009 00:24:40 -0700 (PDT)
>> From: Helen Chen <96258011 at nccu.edu.tw>
>> Subject: [R]  Question of "Quantile Regression for Longitudinal Data"
>> To: r-help at r-project.org
>> Message-ID: <23239896.post at talk.nabble.com>
>> Content-Type: text/plain; charset=us-ascii
>>
>>
>> Hi,
>>
>> I am trying to estimate a quantile regression using panel data. I  
>> am trying
>> to use the model that is described in Dr. Koenker's article. So I  
>> use the
>> code the that is posted in the following link:
>>
>> http://www.econ.uiuc.edu/~roger/research/panel/rq.fit.panel.R
>>
>> How to estimate the panel data quantile regression if the regression
>> contains no constant term? I tried to change the code of  
>> rq.fit.panel by
>> delect "X=cbind(1,x)" and would like to know is that correct ?
>>
>>
>> Thanks
>> I really would appreciate some suggestions.
>> Best
>> Helen Chen
>>
>
> ______________________________________________
> 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.

_____________________________

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag




More information about the R-help mailing list