fjd
fjd

Reputation: 65

R variable parts of a filename

This is a simple rquest, but I can't find anything on the topic. I have a file, lets call it:

infile <- file("clim.bin.1201","rb")

12 is the month and 01 is the day of the month. How can I name the 'infile' so that I don't have to change the 'infile <- file("clim.bin.1201","rb") line, but just change the number of the month and day, which will be defined at the top of the script?

Upvotes: 0

Views: 92

Answers (2)

Richie Cotton
Richie Cotton

Reputation: 121057

As well as JLLagrange's sprintf solution, you can use paste0:

filename <- paste0("clim.bin.", mon, day)
infile <- file(filename, "rb")

Upvotes: 0

colcarroll
colcarroll

Reputation: 3682

You could try

filename <- sprintf("clim.bin.%02d%02d",mon,day)
infile <- file(filename,"rb")

This assumes month and day are numeric.

Upvotes: 1

Related Questions