pbcoletti
pbcoletti

Reputation: 3

Create and Name Multiple files into R

I'm writing a script to create 100 files and name this files with numbers in sequence. ("1.txt", "2.txt", ..., "100.txt")

I tried to use a loop construction, but the file.create() function seems not to support vector args.

I can easily do it one by one, but i'm looking for an automatic solution in order to save time.

Thank you in advance.

Upvotes: 0

Views: 183

Answers (2)

Rich Scriven
Rich Scriven

Reputation: 99331

You don't need to loop, or use regex, or do any type conversion.

You can use a vector created with sprintf() (or paste()) in the ... argument to file.create()

file.create(sprintf("%d.txt", 1:100))

Upvotes: 3

RonaldFindling
RonaldFindling

Reputation: 332

This should do what you need.

s=as.character(seq(from = 1,to=100))
s=sub(x=s, pattern = "(.*)",replacement = "\\1.txt")
file.create(s)

Personally I would use some other language for that like perl/python or just bash.

Upvotes: 1

Related Questions