21

I just cant figure it out how to create a vector in which the strings are constant but the numbers are not. For example:

c("raster[1]","raster[2]","raster[3]")

I'd like to use something like seq(raster[1],raster[99], by=1), but this does not work.

Thanks in advance.

thelatemail
  • 91,185
  • 12
  • 128
  • 188
Agus camacho
  • 868
  • 2
  • 9
  • 24
  • 3
    Why doesn't it work? Post your reproducible attempts so we can help you. If you are constructing code with strings for later evaluation, that's the wrong way to do things. – Rich Scriven Mar 01 '16 at 00:03
  • You can consider looking at answers here for nice discussion and alternatives http://stackoverflow.com/questions/5812493/adding-leading-zeros-using-r – discipulus Jan 28 '17 at 13:18

2 Answers2

31

The sprintf function should also work:

rasters <- sprintf("raster[%s]",seq(1:99))
head(rasters)
[1] "raster[1]" "raster[2]" "raster[3]" "raster[4]" "raster[5]" "raster[6]"

As suggested by Richard Scriven, %d is more efficient than %s. So, if you were working with a longer sequence, it would be more appropriate to use:

rasters <- sprintf("raster[%d]",seq(1:99))
Abdou
  • 12,931
  • 4
  • 39
  • 42
  • 2
    `%d` might b more appropriate. I think with `%s`, the sequence is being coerced to character first and then applied. But I could be wrong. – Rich Scriven Mar 01 '16 at 00:30
  • @RichardScriven, you're on to something. The usage of `%s` definitely makes R coerce the digits into characters. So, `%d` is more efficient. I will add it to the solution. Thanks! – Abdou Mar 01 '16 at 00:38
  • Hate to comment on this but i have the opposite problem where "Ballroom 2-4" would ideally become "Ballroom 2, Ballroom 3, Ballroom 4". Can anyone help me with this ? I already asked the question a couple of days ago on stackoverflow (still waiting for a suggestion) – AofWessex Nov 29 '18 at 01:55
  • @KarimKardous, you may be looking for `paste0(sprintf('Ballroom %d', 1:4), collapse = ", ")`. – Abdou Nov 30 '18 at 21:05
  • Yes your answer makes sense, is there a DYNAMIC way of coding this ? what if the next row's value (it's a dataframe) is "Ballroom 5-8" ? – AofWessex Dec 02 '18 at 01:01
  • @KarimKardous, I can't really think of a dynamic way of doing this. But [here](https://gist.github.com/AbdouSeck/1f024fb740fd6d66038f6ce6389023a8) is a script that tries to answer your question. – Abdou Dec 02 '18 at 17:55
  • I think this gets me far enough in the process. Thank you for your help ! – AofWessex Dec 04 '18 at 21:42
17

We can do

paste0("raster[", 1:6, "]")
# [1] "raster[1]" "raster[2]" "raster[3]" "raster[4]" "raster[5]" "raster[6]"
SymbolixAU
  • 25,502
  • 4
  • 67
  • 139
  • thanks for both answers! both work for me. It is a pity I could not decide which is best, so i just tossed a coin to choose one, though it should be best if at least someone has the mark. I up voted both, though. – Agus camacho Mar 01 '16 at 15:51
  • @Aguscamacho no problem; there's usually more than one way to solve a problem. I wasn't aware of the `sprintf` method either so I've learnt something too. – SymbolixAU Mar 01 '16 at 21:53
  • @Aguscamacho, I think `paste0` is syntactically better than `sprintf` as it's easier to guess what the function aims to achieve just by looking at its name? – Nemo Nov 10 '22 at 23:57