1

I'd like to make a sequence of numbers with a character in R as below.

[H01, H02, H03, ... , H24]

I can only generate numbering characters like

[H1, H2, H3, ... , H24] 

with a command

paste0("H", 1:24)

any tips for this?

jbaums
  • 27,115
  • 5
  • 79
  • 119
KENgerine
  • 33
  • 2

1 Answers1

2

sprintf is similar with printf of C, so you can control the format exactly as below:

sprintf("H%02d", 1:24)
[1] "H01" "H02" "H03" "H04" "H05" "H06" "H07" "H08" "H09" "H10" "H11" "H12" "H13" "H14" "H15" "H16" "H17" "H18" "H19" "H20" "H21"
[22] "H22" "H23" "H24"

Also see this.

Community
  • 1
  • 1
Patric
  • 2,063
  • 17
  • 18
  • Just to nitpick, I'd say that the R `sprintf` is more similar to the C `sprintf`, since it creates an object instead of writing on a file/connection (unless you `print` the result of course). – nicola Jan 04 '16 at 09:34
  • @nicola agree, C' buffer .vs. R' object :) – Patric Jan 04 '16 at 09:37