Q:

write a loop to export each unit of 6*1 vector as text file separately

write a loop to export each unit of 6*1 vector as text file separately
This is my code:
a<-cbind(1,2,3,4,8,9)
a<-data.matrix(a)
a<-t(a)

fn1 <- function (N) 
{
  for(i in 1:6) {
    write.table(N[i],"data.txt")
  }
}
fn1(a)

Question is I want to have 6 text files finally in my directory path(each file contain a number in matrix a), but the file name seems unchanged in the loop code, so I just got a text file. How can I export 6 text files and each text file contains a number in matrix a?

A:

Something like this should work:
a <- cbind(1,2,3,4,8,9)                                                          
a <- data.matrix(a)                                                              
a <- t(a)

fn1 <- function(N) {                                                             
    for(i in 1:6) {                                                              
        file.out <- paste("data", i, ".txt", sep = "")                           
        write.table(N[i], file.out)                                              
    }                                                                            
}    

This will write data1.txt, data2.txt etc...                                                                   

