Avitus
Avitus

Reputation: 744

R: How to produce file names with `write` depending on given parameters

in R I collect n^2 numerical matrices, each matrix depending on a pair of numerical parameters a, b. I would like to write each matrix M in a separate file, specifying in the file name the pair a, b. I am thinking about

write.csv(M,file="M_0_0")

for a==0 b==0 and so on.

Unfortunately I do not know how to automate the process and arrive at the collection "M_a_b"; have you any suggestion regarding the necessary syntax?

I thank you very much!

Upvotes: 0

Views: 53

Answers (1)

Carl Witthoft
Carl Witthoft

Reputation: 21502

As EDi suggested,

for (j in 1:n) {
for (k in 1:n){
    # create or rename your matrix here
    write.csv(M, file= paste("M_",j,"_",k,sep='',collapse=''))
}
}

But unless there's some disastrously bad code elsewhere which insists on having one matrix per file, I'd strongly recommend storing your matrices in as few files as possible, indexed in some sensible way.

Upvotes: 2

Related Questions