Dear R-users,
I'd like to create filenames in a mask "file000.dat" numbered from
1 to e.g.
123. The last problem I'm dealing with is creating the sequence of numbers
with equal length, i.e. 001, 002,.... 023, 024,.... 122, 123.
The closest I got is by a repetition:
Sequence <- c(1:123)
for(i in c(1:length(Sequence))) {
print(
paste(rep("0",
max(nchar(as.character(Sequence)))-nchar(as.character(Sequence[i]))),
as.character(Sequence[i]),
sep=""))
}
where pasting "0"-replication the missing-times I 'm possibly
creating the
desired output. It's just that rep()'s output is not a vector and not
subsequent atoms. and gives "02" and "02" instead of
"002".
Any idea hot to correct the function above or suggestions on
file000.dat-mask filename, please?
Thank you for your time.
M
[[alternative HTML version deleted]]
Henrique Dallazuanna
2010-Jan-25 12:47 UTC
[R] sequence of equal-length numbers (for filenames)
Try sprintf:
sprintf("%03d", Sequence)
sprintf("file%03d.dat", Sequence)
On Mon, Jan 25, 2010 at 10:39 AM, ?rout?k <zroutik at gmail.com>
wrote:> Dear R-users,
>
> I'd like to create filenames in a mask "file000.dat" numbered
from 1 to e.g.
> 123. The last problem I'm dealing with is creating the sequence of
numbers
> with equal length, i.e. 001, 002,.... 023, 024,.... 122, 123.
>
> The closest I got is by a repetition:
>
> Sequence <- c(1:123)
> for(i in c(1:length(Sequence))) {
> print(
> ? ?paste(rep("0",
> max(nchar(as.character(Sequence)))-nchar(as.character(Sequence[i]))),
> ? ?as.character(Sequence[i]),
> ? ?sep=""))
> }
>
> where pasting "0"-replication the missing-times I 'm possibly
creating the
> desired output. It's just that rep()'s output is not a vector and
not
> subsequent atoms. and gives "02" and "02" instead of
"002".
>
> Any idea hot to correct the function above or suggestions on
> file000.dat-mask filename, please?
>
> Thank you for your time.
> M
>
> ? ? ? ?[[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.
>
--
Henrique Dallazuanna
Curitiba-Paran?-Brasil
25? 25' 40" S 49? 16' 22" O