[R] different names for new files in a for loop

Steve Lianoglou mailinglist.honeypot at gmail.com
Tue Jul 28 22:20:16 CEST 2009


Hi,

On Jul 28, 2009, at 3:43 PM, Yash Gandhi wrote:

>
> Hi,
>
> I have an excel file with 10 columns and I am trying to create new  
> excel files each with columns 1, 2, and columns 3-10.
> Does anyone know how to change the name of the file in a for loop so  
> that the first new file will have columns 1, 2, 3 with a name and  
> then the next file will have columns 1, 2, 4 with a different name  
> and so on till i get to the 8th new file with columns 1, 2, 10?

Hmm ... I don't ever deal with excel files directly in R, so I'll  
assume you have a csv file (or some other delimited file) -- same  
thing, no? Yes:

file.columns <- 3:10
my.data <- read.table('myfile.txt', OTHER, OPTIONS, HERE, ...)
for (i in 3:10) {
   filename <- paste('splitfile', i, 'txt', sep='.')
   write.table(my.data[,c(1,2,i)], OTHER, OPTIONS, HERE, file=filename)
   ...
}

See ?write.table for possible "OTHER OPTIONS" you can use in the calls  
to read/write.table.

-steve

--
Steve Lianoglou
Graduate Student: Computational Systems Biology
   |  Memorial Sloan-Kettering Cancer Center
   |  Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact




More information about the R-help mailing list