how create a sequence of strings with different numbers in R
We can do
paste0("raster[", 1:6, "]")
# [1] "raster[1]" "raster[2]" "raster[3]" "raster[4]" "raster[5]" "raster[6]"
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))